Skip to content

Commit 292d253

Browse files
committed
feat: ^_^
Signed-off-by: Luozhou (Siyuan Cao) <luozhou.csy@alibaba-inc.com>
1 parent 064a938 commit 292d253

10 files changed

+1088
-0
lines changed

1.两数之和.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=1 lang=typescript
3+
*
4+
* [1] 两数之和
5+
*
6+
* https://leetcode.cn/problems/two-sum/description/
7+
*
8+
* algorithms
9+
* Easy (52.77%)
10+
* Likes: 15116
11+
* Dislikes: 0
12+
* Total Accepted: 3.7M
13+
* Total Submissions: 7M
14+
* Testcase Example: '[2,7,11,15]\n9'
15+
*
16+
* 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标。
17+
*
18+
* 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
19+
*
20+
* 你可以按任意顺序返回答案。
21+
*
22+
*
23+
*
24+
* 示例 1:
25+
*
26+
*
27+
* 输入:nums = [2,7,11,15], target = 9
28+
* 输出:[0,1]
29+
* 解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
30+
*
31+
*
32+
* 示例 2:
33+
*
34+
*
35+
* 输入:nums = [3,2,4], target = 6
36+
* 输出:[1,2]
37+
*
38+
*
39+
* 示例 3:
40+
*
41+
*
42+
* 输入:nums = [3,3], target = 6
43+
* 输出:[0,1]
44+
*
45+
*
46+
*
47+
*
48+
* 提示:
49+
*
50+
*
51+
* 2 <= nums.length <= 10^4
52+
* -10^9 <= nums[i] <= 10^9
53+
* -10^9 <= target <= 10^9
54+
* 只会存在一个有效答案
55+
*
56+
*
57+
* 进阶:你可以想出一个时间复杂度小于 O(n^2) 的算法吗?
58+
*
59+
*/
60+
61+
export
62+
// @lc code=start
63+
function twoSum(nums: number[], target: number): number[] {
64+
const seen: Record<number, number> = {}
65+
66+
for (let i = 0; i < nums.length; i++) {
67+
const n = nums[i]
68+
const desired = target - n
69+
70+
if (typeof seen[desired] === 'number') {
71+
return [seen[desired], i]
72+
}
73+
74+
seen[n] = i
75+
}
76+
77+
return [-1, -1]
78+
}
79+
// @lc code=end
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* @lc app=leetcode.cn id=138 lang=typescript
3+
*
4+
* [138] 复制带随机指针的链表
5+
*
6+
* https://leetcode.cn/problems/copy-list-with-random-pointer/description/
7+
*
8+
* algorithms
9+
* Medium (67.04%)
10+
* Likes: 969
11+
* Dislikes: 0
12+
* Total Accepted: 165.5K
13+
* Total Submissions: 246.9K
14+
* Testcase Example: '[[7,null],[13,0],[11,4],[10,2],[1,0]]'
15+
*
16+
* 给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。
17+
*
18+
* 构造这个链表的 深拷贝。 深拷贝应该正好由 n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random
19+
* 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 。
20+
*
21+
* 例如,如果原链表中有 X 和 Y 两个节点,其中 X.random --> Y 。那么在复制链表中对应的两个节点 x 和 y ,同样有 x.random
22+
* --> y 。
23+
*
24+
* 返回复制链表的头节点。
25+
*
26+
* 用一个由 n 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 [val, random_index] 表示:
27+
*
28+
*
29+
* val:一个表示 Node.val 的整数。
30+
* random_index:随机指针指向的节点索引(范围从 0 到 n-1);如果不指向任何节点,则为  null 。
31+
*
32+
*
33+
* 你的代码 只 接受原链表的头节点 head 作为传入参数。
34+
*
35+
*
36+
*
37+
* 示例 1:
38+
*
39+
*
40+
*
41+
*
42+
* 输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
43+
* 输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]
44+
*
45+
*
46+
* 示例 2:
47+
*
48+
*
49+
*
50+
*
51+
* 输入:head = [[1,1],[2,1]]
52+
* 输出:[[1,1],[2,1]]
53+
*
54+
*
55+
* 示例 3:
56+
*
57+
*
58+
*
59+
*
60+
* 输入:head = [[3,null],[3,0],[3,null]]
61+
* 输出:[[3,null],[3,0],[3,null]]
62+
*
63+
*
64+
*
65+
*
66+
* 提示:
67+
*
68+
*
69+
* 0 <= n <= 1000
70+
* -10^4 <= Node.val <= 10^4
71+
* Node.random 为 null 或指向链表中的节点。
72+
*
73+
*
74+
*/
75+
76+
class Node {
77+
val: number
78+
79+
next: Node | null
80+
81+
random: Node | null
82+
83+
constructor(val?: number, next?: Node, random?: Node) {
84+
this.val = (val === undefined ? 0 : val)
85+
this.next = (next === undefined ? null : next)
86+
this.random = (random === undefined ? null : random)
87+
}
88+
}
89+
90+
export
91+
// @lc code=start
92+
function copyRandomList(head: Node | null): Node | null {
93+
// copy all nodes to the next of original node
94+
let curr = head
95+
while (curr) {
96+
const cloned = new Node(curr.val)
97+
const { next } = curr
98+
99+
curr.next = cloned
100+
cloned.next = next
101+
curr = next
102+
}
103+
104+
// point all random pointers
105+
curr = head
106+
while (curr) {
107+
const { random, next: cloned } = curr
108+
const next = cloned!.next
109+
110+
if (random) {
111+
cloned!.random = random.next
112+
}
113+
114+
curr = next
115+
}
116+
117+
// split two list
118+
const dummy = new Node()
119+
let prevCloned = dummy
120+
curr = head
121+
while (curr) {
122+
const { next: cloned } = curr
123+
const next = cloned!.next
124+
125+
prevCloned.next = cloned!
126+
prevCloned = prevCloned.next!
127+
prevCloned.next = null
128+
129+
curr.next = next
130+
curr = next
131+
}
132+
133+
return dummy.next
134+
}
135+
// @lc code=end
136+
137+
function getClonedOrCreate(map: Map<Node, Node>, original: Node | null) {
138+
if (!original) return null
139+
140+
const cloned = map.get(original) || new Node(original.val)
141+
map.set(original, cloned)
142+
return cloned
143+
}
144+
145+
function copyRandomList2(head: Node | null): Node | null {
146+
const map = new Map<Node, Node>()
147+
const dummy = new Node()
148+
let curr = head
149+
let prev = dummy
150+
151+
while (curr) {
152+
const { random, next } = curr
153+
154+
const cloned = getClonedOrCreate(map, curr)!
155+
cloned.random = getClonedOrCreate(map, random)
156+
cloned.next = getClonedOrCreate(map, next)
157+
158+
prev.next = cloned
159+
prev = prev.next
160+
161+
curr = curr.next
162+
}
163+
164+
return dummy.next
165+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* @lc app=leetcode.cn id=1448 lang=typescript
3+
*
4+
* [1448] 统计二叉树中好节点的数目
5+
*
6+
* https://leetcode.cn/problems/count-good-nodes-in-binary-tree/description/
7+
*
8+
* algorithms
9+
* Medium (72.16%)
10+
* Likes: 59
11+
* Dislikes: 0
12+
* Total Accepted: 15.6K
13+
* Total Submissions: 21.6K
14+
* Testcase Example: '[3,1,4,3,null,1,5]'
15+
*
16+
* 给你一棵根为 root 的二叉树,请你返回二叉树中好节点的数目。
17+
*
18+
* 「好节点」X 定义为:从根到该节点 X 所经过的节点中,没有任何节点的值大于 X 的值。
19+
*
20+
*
21+
*
22+
* 示例 1:
23+
*
24+
*
25+
*
26+
* 输入:root = [3,1,4,3,null,1,5]
27+
* 输出:4
28+
* 解释:图中蓝色节点为好节点。
29+
* 根节点 (3) 永远是个好节点。
30+
* 节点 4 -> (3,4) 是路径中的最大值。
31+
* 节点 5 -> (3,4,5) 是路径中的最大值。
32+
* 节点 3 -> (3,1,3) 是路径中的最大值。
33+
*
34+
* 示例 2:
35+
*
36+
*
37+
*
38+
* 输入:root = [3,3,null,4,2]
39+
* 输出:3
40+
* 解释:节点 2 -> (3, 3, 2) 不是好节点,因为 "3" 比它大。
41+
*
42+
* 示例 3:
43+
*
44+
* 输入:root = [1]
45+
* 输出:1
46+
* 解释:根节点是好节点。
47+
*
48+
*
49+
*
50+
* 提示:
51+
*
52+
*
53+
* 二叉树中节点数目范围是 [1, 10^5] 。
54+
* 每个节点权值的范围是 [-10^4, 10^4] 。
55+
*
56+
*
57+
*/
58+
59+
// @lc code=start
60+
function goodNodes(root: TreeNode | null): number {
61+
let result = 0
62+
63+
const walk = (node: TreeNode | null, maxValue: number) => {
64+
if (!node) return
65+
66+
if (maxValue <= node.val) {
67+
result += 1
68+
}
69+
70+
const nextMaxVal = Math.max(maxValue, node.val)
71+
walk(node.left, nextMaxVal)
72+
walk(node.right, nextMaxVal)
73+
}
74+
75+
walk(root, Number.MIN_SAFE_INTEGER)
76+
return result
77+
}
78+
// @lc code=end

0 commit comments

Comments
 (0)