Skip to content

Commit 7b0c450

Browse files
committed
feat: ^_^
Signed-off-by: Luozhou (Siyuan Cao) <luozhou.csy@alibaba-inc.com>
1 parent 058895e commit 7b0c450

File tree

58 files changed

+2971
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2971
-3
lines changed

114.二叉树展开为链表.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* @lc app=leetcode.cn id=114 lang=typescript
3+
*
4+
* [114] 二叉树展开为链表
5+
*
6+
* https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/description/
7+
*
8+
* algorithms
9+
* Medium (72.70%)
10+
* Likes: 1038
11+
* Dislikes: 0
12+
* Total Accepted: 207.4K
13+
* Total Submissions: 285.2K
14+
* Testcase Example: '[1,2,5,3,4,null,6]'
15+
*
16+
* 给你二叉树的根结点 root ,请你将它展开为一个单链表:
17+
*
18+
*
19+
* 展开后的单链表应该同样使用 TreeNode ,其中 right 子指针指向链表中下一个结点,而左子指针始终为 null 。
20+
* 展开后的单链表应该与二叉树 先序遍历 顺序相同。
21+
*
22+
*
23+
*
24+
*
25+
* 示例 1:
26+
*
27+
*
28+
* 输入:root = [1,2,5,3,4,null,6]
29+
* 输出:[1,null,2,null,3,null,4,null,5,null,6]
30+
*
31+
*
32+
* 示例 2:
33+
*
34+
*
35+
* 输入:root = []
36+
* 输出:[]
37+
*
38+
*
39+
* 示例 3:
40+
*
41+
*
42+
* 输入:root = [0]
43+
* 输出:[0]
44+
*
45+
*
46+
*
47+
*
48+
* 提示:
49+
*
50+
*
51+
* 树中结点数在范围 [0, 2000] 内
52+
* -100
53+
*
54+
*
55+
*
56+
*
57+
* 进阶:你可以使用原地算法(O(1) 额外空间)展开这棵树吗?
58+
*
59+
*/
60+
61+
import { TreeNode } from './commons/Tree'
62+
63+
export
64+
// @lc code=start
65+
function flatten(root?: TreeNode | null): void {
66+
if (!root) return
67+
68+
if (root.left) {
69+
if (root.right) {
70+
const right = root.right
71+
findPreOfRightChildren(root).right = right
72+
root.right = root.left
73+
} else {
74+
root.right = root.left
75+
}
76+
root.left = null
77+
}
78+
79+
flatten(root?.right)
80+
}
81+
82+
function findPreOfRightChildren(node: TreeNode) {
83+
let curr = node.left!
84+
85+
while (curr.right) {
86+
curr = curr.right
87+
}
88+
89+
return curr
90+
}
91+
// @lc code=end
92+
93+
/**
94+
1
95+
\
96+
2
97+
/
98+
3
99+
*/
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* @lc app=leetcode.cn id=124 lang=typescript
3+
*
4+
* [124] 二叉树中的最大路径和
5+
*
6+
* https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/description/
7+
*
8+
* algorithms
9+
* Hard (44.67%)
10+
* Likes: 1389
11+
* Dislikes: 0
12+
* Total Accepted: 184.7K
13+
* Total Submissions: 413.4K
14+
* Testcase Example: '[1,2,3]'
15+
*
16+
* 路径 被定义为一条从树中任意节点出发,沿父节点-子节点连接,达到任意节点的序列。同一个节点在一条路径序列中 至多出现一次 。该路径 至少包含一个
17+
* 节点,且不一定经过根节点。
18+
*
19+
* 路径和 是路径中各节点值的总和。
20+
*
21+
* 给你一个二叉树的根节点 root ,返回其 最大路径和 。
22+
*
23+
*
24+
*
25+
* 示例 1:
26+
*
27+
*
28+
* 输入:root = [1,2,3]
29+
* 输出:6
30+
* 解释:最优路径是 2 -> 1 -> 3 ,路径和为 2 + 1 + 3 = 6
31+
*
32+
* 示例 2:
33+
*
34+
*
35+
* 输入:root = [-10,9,20,null,null,15,7]
36+
* 输出:42
37+
* 解释:最优路径是 15 -> 20 -> 7 ,路径和为 15 + 20 + 7 = 42
38+
*
39+
*
40+
*
41+
*
42+
* 提示:
43+
*
44+
*
45+
* 树中节点数目范围是 [1, 3 * 10^4]
46+
* -1000
47+
*
48+
*
49+
*/
50+
51+
import { TreeNode } from './commons/Tree'
52+
53+
export
54+
// @lc code=start
55+
function maxPathSum(root: TreeNode | null): number {
56+
let result = Number.MIN_SAFE_INTEGER
57+
58+
function helper(node: TreeNode): number {
59+
const leftResult = node.left ? helper(node.left) : 0
60+
const rightResult = node.right ? helper(node.right) : 0
61+
62+
result = Math.max(
63+
node.val,
64+
node.val + leftResult,
65+
node.val + rightResult,
66+
node.val + leftResult + rightResult,
67+
result,
68+
)
69+
70+
return Math.max(
71+
node.val,
72+
node.val + leftResult,
73+
node.val + rightResult,
74+
)
75+
}
76+
helper(root!)
77+
78+
return result
79+
}
80+
// @lc code=end

125.验证回文串.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* @lc app=leetcode.cn id=125 lang=typescript
3+
*
4+
* [125] 验证回文串
5+
*
6+
* https://leetcode-cn.com/problems/valid-palindrome/description/
7+
*
8+
* algorithms
9+
* Easy (47.12%)
10+
* Likes: 460
11+
* Dislikes: 0
12+
* Total Accepted: 312.6K
13+
* Total Submissions: 663.4K
14+
* Testcase Example: '"A man, a plan, a canal: Panama"'
15+
*
16+
* 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
17+
*
18+
* 说明:本题中,我们将空字符串定义为有效的回文串。
19+
*
20+
*
21+
*
22+
* 示例 1:
23+
*
24+
*
25+
* 输入: "A man, a plan, a canal: Panama"
26+
* 输出: true
27+
* 解释:"amanaplanacanalpanama" 是回文串
28+
*
29+
*
30+
* 示例 2:
31+
*
32+
*
33+
* 输入: "race a car"
34+
* 输出: false
35+
* 解释:"raceacar" 不是回文串
36+
*
37+
*
38+
*
39+
*
40+
* 提示:
41+
*
42+
*
43+
* 1
44+
* 字符串 s 由 ASCII 字符组成
45+
*
46+
*
47+
*/
48+
49+
export
50+
// @lc code=start
51+
function isPalindrome(s: string): boolean {
52+
let left = 0, right = s.length - 1
53+
while (left < right) {
54+
while (left < right && !isNumAlpha(s[left])) left++
55+
if (left === right) break
56+
57+
while (left < right && !isNumAlpha(s[right])) right--
58+
if (left === right) break
59+
60+
if (s[left].toLowerCase() !== s[right].toLowerCase()) {
61+
return false
62+
}
63+
left++
64+
right--
65+
}
66+
67+
return true
68+
}
69+
70+
const CharCodes = {
71+
a: 'a'.charCodeAt(0),
72+
z: 'z'.charCodeAt(0),
73+
A: 'A'.charCodeAt(0),
74+
Z: 'Z'.charCodeAt(0),
75+
0: '0'.charCodeAt(0),
76+
9: '9'.charCodeAt(0),
77+
}
78+
79+
function isNumAlpha(char: string) {
80+
const charCode = char.charCodeAt(0)
81+
return (charCode >= CharCodes.a && charCode <= CharCodes.z)
82+
|| (charCode >= CharCodes.A && charCode <= CharCodes.Z)
83+
|| (charCode >= CharCodes['0'] && charCode <= CharCodes['9'])
84+
}
85+
// @lc code=end

133.克隆图.ts

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* @lc app=leetcode.cn id=133 lang=typescript
3+
*
4+
* [133] 克隆图
5+
*
6+
* https://leetcode-cn.com/problems/clone-graph/description/
7+
*
8+
* algorithms
9+
* Medium (67.87%)
10+
* Likes: 456
11+
* Dislikes: 0
12+
* Total Accepted: 80.9K
13+
* Total Submissions: 119.1K
14+
* Testcase Example: '[[2,4],[1,3],[2,4],[1,3]]'
15+
*
16+
* 给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆)。
17+
*
18+
* 图中的每个节点都包含它的值 val(int) 和其邻居的列表(list[Node])。
19+
*
20+
* class Node {
21+
* ⁠ public int val;
22+
* ⁠ public List<Node> neighbors;
23+
* }
24+
*
25+
*
26+
*
27+
* 测试用例格式:
28+
*
29+
* 简单起见,每个节点的值都和它的索引相同。例如,第一个节点值为 1(val = 1),第二个节点值为 2(val =
30+
* 2),以此类推。该图在测试用例中使用邻接列表表示。
31+
*
32+
* 邻接列表 是用于表示有限图的无序列表的集合。每个列表都描述了图中节点的邻居集。
33+
*
34+
* 给定节点将始终是图中的第一个节点(值为 1)。你必须将 给定节点的拷贝 作为对克隆图的引用返回。
35+
*
36+
*
37+
*
38+
* 示例 1:
39+
*
40+
*
41+
*
42+
* 输入:adjList = [[2,4],[1,3],[2,4],[1,3]]
43+
* 输出:[[2,4],[1,3],[2,4],[1,3]]
44+
* 解释:
45+
* 图中有 4 个节点。
46+
* 节点 1 的值是 1,它有两个邻居:节点 2 和 4 。
47+
* 节点 2 的值是 2,它有两个邻居:节点 1 和 3 。
48+
* 节点 3 的值是 3,它有两个邻居:节点 2 和 4 。
49+
* 节点 4 的值是 4,它有两个邻居:节点 1 和 3 。
50+
*
51+
*
52+
* 示例 2:
53+
*
54+
*
55+
*
56+
* 输入:adjList = [[]]
57+
* 输出:[[]]
58+
* 解释:输入包含一个空列表。该图仅仅只有一个值为 1 的节点,它没有任何邻居。
59+
*
60+
*
61+
* 示例 3:
62+
*
63+
* 输入:adjList = []
64+
* 输出:[]
65+
* 解释:这个图是空的,它不含任何节点。
66+
*
67+
*
68+
* 示例 4:
69+
*
70+
*
71+
*
72+
* 输入:adjList = [[2],[1]]
73+
* 输出:[[2],[1]]
74+
*
75+
*
76+
*
77+
* 提示:
78+
*
79+
*
80+
* 节点数不超过 100 。
81+
* 每个节点值 Node.val 都是唯一的,1 <= Node.val <= 100。
82+
* 无向图是一个简单图,这意味着图中没有重复的边,也没有自环。
83+
* 由于图是无向的,如果节点 p 是节点 q 的邻居,那么节点 q 也必须是节点 p 的邻居。
84+
* 图是连通图,你可以从给定节点访问到所有节点。
85+
*
86+
*
87+
*/
88+
89+
import { GraphNode } from './commons/GraphNode'
90+
91+
const Node = GraphNode
92+
// eslint-disable-next-line @typescript-eslint/no-redeclare
93+
type Node = GraphNode<number>
94+
95+
export
96+
// @lc code=start
97+
function cloneGraph(node: Node | null): Node | null {
98+
if (!node) return null
99+
100+
const queue: Node[] = [node]
101+
const seen = new Set<Node>(queue)
102+
const map = new Map<Node, Node>()
103+
104+
function buildOrGetNode(n: Node) {
105+
if (!map.get(n)) {
106+
map.set(n, new Node(n.val))
107+
}
108+
return map.get(n)!
109+
}
110+
111+
while (queue.length) {
112+
const curr = queue.shift()!
113+
const currCloned = buildOrGetNode(curr)
114+
115+
for (const n of curr.neighbors) {
116+
if (!seen.has(n)) {
117+
seen.add(n)
118+
queue.push(n)
119+
}
120+
currCloned.neighbors.push(buildOrGetNode(n))
121+
}
122+
}
123+
124+
return map.get(node)!
125+
}
126+
// @lc code=end

0 commit comments

Comments
 (0)