Skip to content

Commit 93b303c

Browse files
committed
feat: ^_^
Signed-off-by: Luozhou (Siyuan Cao) <luozhou.csy@alibaba-inc.com>
1 parent 6a9d7ae commit 93b303c

20 files changed

+1020
-1
lines changed

139.单词拆分.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* @lc app=leetcode.cn id=139 lang=typescript
3+
*
4+
* [139] 单词拆分
5+
*
6+
* https://leetcode-cn.com/problems/word-break/description/
7+
*
8+
* algorithms
9+
* Medium (52.11%)
10+
* Likes: 1340
11+
* Dislikes: 0
12+
* Total Accepted: 229.8K
13+
* Total Submissions: 440.9K
14+
* Testcase Example: '"leetcode"\n["leet","code"]'
15+
*
16+
* 给你一个字符串 s 和一个字符串列表 wordDict 作为字典。请你判断是否可以利用字典中出现的单词拼接出 s 。
17+
*
18+
* 注意:不要求字典中出现的单词全部都使用,并且字典中的单词可以重复使用。
19+
*
20+
*
21+
*
22+
* 示例 1:
23+
*
24+
*
25+
* 输入: s = "leetcode", wordDict = ["leet", "code"]
26+
* 输出: true
27+
* 解释: 返回 true 因为 "leetcode" 可以由 "leet" 和 "code" 拼接成。
28+
*
29+
*
30+
* 示例 2:
31+
*
32+
*
33+
* 输入: s = "applepenapple", wordDict = ["apple", "pen"]
34+
* 输出: true
35+
* 解释: 返回 true 因为 "applepenapple" 可以由 "apple" "pen" "apple" 拼接成。
36+
* 注意,你可以重复使用字典中的单词。
37+
*
38+
*
39+
* 示例 3:
40+
*
41+
*
42+
* 输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
43+
* 输出: false
44+
*
45+
*
46+
*
47+
*
48+
* 提示:
49+
*
50+
*
51+
* 1 <= s.length <= 300
52+
* 1 <= wordDict.length <= 1000
53+
* 1 <= wordDict[i].length <= 20
54+
* s 和 wordDict[i] 仅有小写英文字母组成
55+
* wordDict 中的所有字符串 互不相同
56+
*
57+
*
58+
*/
59+
60+
export
61+
// @lc code=start
62+
function wordBreak(s: string, wordDict: string[]): boolean {
63+
const words = new Set(wordDict)
64+
const dp = []
65+
for (let i = 0; i < s.length; i++) {
66+
let canMatch = false
67+
for (let j = 0; j <= i; j++) {
68+
const prevMatch = j === 0 || dp[j - 1]
69+
if (prevMatch && words.has(s.slice(j, i + 1))) {
70+
canMatch = true
71+
break
72+
}
73+
}
74+
dp.push(canMatch)
75+
}
76+
77+
return dp[s.length - 1]
78+
}
79+
// @lc code=end

155.最小栈.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* @lc app=leetcode.cn id=155 lang=typescript
3+
*
4+
* [155] 最小栈
5+
*
6+
* https://leetcode-cn.com/problems/min-stack/description/
7+
*
8+
* algorithms
9+
* Easy (57.62%)
10+
* Likes: 1147
11+
* Dislikes: 0
12+
* Total Accepted: 323.7K
13+
* Total Submissions: 561.7K
14+
* Testcase Example: '["MinStack","push","push","push","getMin","pop","top","getMin"]\n' +
15+
'[[],[-2],[0],[-3],[],[],[],[]]'
16+
*
17+
* 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
18+
*
19+
*
20+
* push(x) —— 将元素 x 推入栈中。
21+
* pop() —— 删除栈顶的元素。
22+
* top() —— 获取栈顶元素。
23+
* getMin() —— 检索栈中的最小元素。
24+
*
25+
*
26+
*
27+
*
28+
* 示例:
29+
*
30+
* 输入:
31+
* ["MinStack","push","push","push","getMin","pop","top","getMin"]
32+
* [[],[-2],[0],[-3],[],[],[],[]]
33+
*
34+
* 输出:
35+
* [null,null,null,null,-3,null,0,-2]
36+
*
37+
* 解释:
38+
* MinStack minStack = new MinStack();
39+
* minStack.push(-2);
40+
* minStack.push(0);
41+
* minStack.push(-3);
42+
* minStack.getMin(); --> 返回 -3.
43+
* minStack.pop();
44+
* minStack.top(); --> 返回 0.
45+
* minStack.getMin(); --> 返回 -2.
46+
*
47+
*
48+
*
49+
*
50+
* 提示:
51+
*
52+
*
53+
* pop、top 和 getMin 操作总是在 非空栈 上调用。
54+
*
55+
*
56+
*/
57+
58+
export
59+
// @lc code=start
60+
class MinStack {
61+
private readonly nums: number[] = []
62+
63+
private readonly min: number[] = []
64+
65+
push(val: number): void {
66+
this.nums.push(val)
67+
if (!this.min.length) {
68+
this.min.push(val)
69+
} else {
70+
const prevMin = this.min[this.min.length - 1]
71+
this.min.push(Math.min(prevMin, val))
72+
}
73+
}
74+
75+
pop(): void {
76+
this.min.pop()
77+
this.nums.pop()
78+
}
79+
80+
top(): number {
81+
return this.nums[this.nums.length - 1]
82+
}
83+
84+
getMin(): number {
85+
return this.min[this.min.length - 1]
86+
}
87+
}
88+
89+
/**
90+
* Your MinStack object will be instantiated and called as such:
91+
* var obj = new MinStack()
92+
* obj.push(val)
93+
* obj.pop()
94+
* var param_3 = obj.top()
95+
* var param_4 = obj.getMin()
96+
*/
97+
// @lc code=end
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* @lc app=leetcode.cn id=19 lang=typescript
3+
*
4+
* [19] 删除链表的倒数第 N 个结点
5+
*
6+
* https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/description/
7+
*
8+
* algorithms
9+
* Medium (43.37%)
10+
* Likes: 1765
11+
* Dislikes: 0
12+
* Total Accepted: 626.2K
13+
* Total Submissions: 1.4M
14+
* Testcase Example: '[1,2,3,4,5]\n2'
15+
*
16+
* 给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
17+
*
18+
*
19+
*
20+
* 示例 1:
21+
*
22+
*
23+
* 输入:head = [1,2,3,4,5], n = 2
24+
* 输出:[1,2,3,5]
25+
*
26+
*
27+
* 示例 2:
28+
*
29+
*
30+
* 输入:head = [1], n = 1
31+
* 输出:[]
32+
*
33+
*
34+
* 示例 3:
35+
*
36+
*
37+
* 输入:head = [1,2], n = 1
38+
* 输出:[1]
39+
*
40+
*
41+
*
42+
*
43+
* 提示:
44+
*
45+
*
46+
* 链表中结点的数目为 sz
47+
* 1 <= sz <= 30
48+
* 0 <= Node.val <= 100
49+
* 1 <= n <= sz
50+
*
51+
*
52+
*
53+
*
54+
* 进阶:你能尝试使用一趟扫描实现吗?
55+
*
56+
*/
57+
58+
import { ListNode } from './commons/list'
59+
60+
export
61+
// @lc code=start
62+
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
63+
const dummy = new ListNode(-1, head)
64+
65+
let fast = dummy
66+
for (let i = 0; i < n; i++) {
67+
fast = fast.next!
68+
}
69+
70+
let slow: ListNode = dummy
71+
while (fast.next) {
72+
slow = slow.next!
73+
fast = fast.next
74+
}
75+
76+
slow.next = slow.next!.next
77+
return dummy.next
78+
}
79+
// @lc code=end

232.用栈实现队列.ts

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* @lc app=leetcode.cn id=232 lang=typescript
3+
*
4+
* [232] 用栈实现队列
5+
*
6+
* https://leetcode-cn.com/problems/implement-queue-using-stacks/description/
7+
*
8+
* algorithms
9+
* Easy (68.98%)
10+
* Likes: 559
11+
* Dislikes: 0
12+
* Total Accepted: 194.1K
13+
* Total Submissions: 281.4K
14+
* Testcase Example: '["MyQueue","push","push","peek","pop","empty"]\n[[],[1],[2],[],[],[]]'
15+
*
16+
* 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
17+
*
18+
* 实现 MyQueue 类:
19+
*
20+
*
21+
* void push(int x) 将元素 x 推到队列的末尾
22+
* int pop() 从队列的开头移除并返回元素
23+
* int peek() 返回队列开头的元素
24+
* boolean empty() 如果队列为空,返回 true ;否则,返回 false
25+
*
26+
*
27+
* 说明:
28+
*
29+
*
30+
* 你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty
31+
* 操作是合法的。
32+
* 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
33+
*
34+
*
35+
*
36+
*
37+
* 示例 1:
38+
*
39+
*
40+
* 输入:
41+
* ["MyQueue", "push", "push", "peek", "pop", "empty"]
42+
* [[], [1], [2], [], [], []]
43+
* 输出:
44+
* [null, null, null, 1, 1, false]
45+
*
46+
* 解释:
47+
* MyQueue myQueue = new MyQueue();
48+
* myQueue.push(1); // queue is: [1]
49+
* myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
50+
* myQueue.peek(); // return 1
51+
* myQueue.pop(); // return 1, queue is [2]
52+
* myQueue.empty(); // return false
53+
*
54+
*
55+
*
56+
*
57+
*
58+
*
59+
*
60+
* 提示:
61+
*
62+
*
63+
* 1 <= x <= 9
64+
* 最多调用 100 次 push、pop、peek 和 empty
65+
* 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)
66+
*
67+
*
68+
*
69+
*
70+
* 进阶:
71+
*
72+
*
73+
* 你能否实现每个操作均摊时间复杂度为 O(1) 的队列?换句话说,执行 n 个操作的总时间复杂度为 O(n) ,即使其中一个操作可能花费较长时间。
74+
*
75+
*
76+
*/
77+
78+
export
79+
// @lc code=start
80+
class MyQueue {
81+
private readonly stack1: number[] = []
82+
83+
private readonly stack2: number[] = []
84+
85+
push(x: number): void {
86+
this.stack1.push(x)
87+
}
88+
89+
private normalize() {
90+
if (this.stack2.length) {
91+
return
92+
}
93+
94+
while (this.stack1.length) {
95+
this.stack2.push(this.stack1.pop()!)
96+
}
97+
}
98+
99+
pop(): number {
100+
this.normalize()
101+
return this.stack2.pop()!
102+
}
103+
104+
peek(): number {
105+
this.normalize()
106+
return this.stack2[this.stack2.length - 1]
107+
}
108+
109+
empty(): boolean {
110+
return !this.stack1.length && !this.stack2.length
111+
}
112+
}
113+
114+
/**
115+
* Your MyQueue object will be instantiated and called as such:
116+
* var obj = new MyQueue()
117+
* obj.push(x)
118+
* var param_2 = obj.pop()
119+
* var param_3 = obj.peek()
120+
* var param_4 = obj.empty()
121+
*/
122+
// @lc code=end

0 commit comments

Comments
 (0)