Skip to content

Commit cddcef0

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

20 files changed

+1833
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* @lc app=leetcode.cn id=105 lang=typescript
3+
*
4+
* [105] 从前序与中序遍历序列构造二叉树
5+
*
6+
* https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/
7+
*
8+
* algorithms
9+
* Medium (71.32%)
10+
* Likes: 1699
11+
* Dislikes: 0
12+
* Total Accepted: 406.1K
13+
* Total Submissions: 569.3K
14+
* Testcase Example: '[3,9,20,15,7]\n[9,3,15,20,7]'
15+
*
16+
* 给定两个整数数组 preorder 和 inorder ,其中 preorder 是二叉树的先序遍历, inorder
17+
* 是同一棵树的中序遍历,请构造二叉树并返回其根节点。
18+
*
19+
*
20+
*
21+
* 示例 1:
22+
*
23+
*
24+
* 输入: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
25+
* 输出: [3,9,20,null,null,15,7]
26+
*
27+
*
28+
* 示例 2:
29+
*
30+
*
31+
* 输入: preorder = [-1], inorder = [-1]
32+
* 输出: [-1]
33+
*
34+
*
35+
*
36+
*
37+
* 提示:
38+
*
39+
*
40+
* 1 <= preorder.length <= 3000
41+
* inorder.length == preorder.length
42+
* -3000 <= preorder[i], inorder[i] <= 3000
43+
* preorder 和 inorder 均 无重复 元素
44+
* inorder 均出现在 preorder
45+
* preorder 保证 为二叉树的前序遍历序列
46+
* inorder 保证 为二叉树的中序遍历序列
47+
*
48+
*
49+
*/
50+
51+
import { TreeNode } from './commons/Tree'
52+
53+
export
54+
// @lc code=start
55+
function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
56+
const inOrderIndexMapping: Record<number, number> = {}
57+
inorder.forEach((n, i) => {
58+
inOrderIndexMapping[n] = i
59+
})
60+
61+
function build(preBegin: number, preEnd: number, inBegin: number, inEnd: number) {
62+
if (preBegin > preEnd) return null
63+
64+
const rootVal = preorder[preBegin]
65+
const inRoot = inOrderIndexMapping[rootVal]
66+
67+
const leftLength = inRoot - 1 - inBegin + 1
68+
69+
const leftPreBegin = preBegin + 1
70+
const leftPreEnd = leftPreBegin + leftLength - 1
71+
const rightPreBegin = leftPreEnd + 1
72+
const rightPreEnd = preEnd
73+
74+
const rootNode = new TreeNode(rootVal)
75+
rootNode.left = build(leftPreBegin, leftPreEnd, inBegin, inRoot - 1)
76+
rootNode.right = build(rightPreBegin, rightPreEnd, inRoot + 1, inEnd)
77+
return rootNode
78+
}
79+
80+
const m = preorder.length
81+
return build(0, m - 1, 0, m - 1)
82+
}
83+
// @lc code=end
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* @lc app=leetcode.cn id=124 lang=typescript
3+
*
4+
* [124] 二叉树中的最大路径和
5+
*
6+
* https://leetcode.cn/problems/binary-tree-maximum-path-sum/description/
7+
*
8+
* algorithms
9+
* Hard (45.21%)
10+
* Likes: 1687
11+
* Dislikes: 0
12+
* Total Accepted: 263.6K
13+
* Total Submissions: 583K
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 = root?.val ?? Number.MIN_SAFE_INTEGER
57+
58+
function helper(node: TreeNode | null) {
59+
if (!node) return Number.MIN_SAFE_INTEGER
60+
61+
const leftResult = helper(node.left)
62+
const rightResult = helper(node.right)
63+
64+
const singleLineResult = Math.max(
65+
node.val,
66+
leftResult + node.val,
67+
rightResult + node.val,
68+
)
69+
70+
result = Math.max(
71+
result,
72+
singleLineResult,
73+
node.val + leftResult + rightResult,
74+
)
75+
return singleLineResult
76+
}
77+
78+
helper(root)
79+
return result
80+
}
81+
// @lc code=end

128.最长连续序列.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#
2+
# @lc app=leetcode.cn id=128 lang=python3
3+
#
4+
# [128] 最长连续序列
5+
#
6+
# https://leetcode.cn/problems/longest-consecutive-sequence/description/
7+
#
8+
# algorithms
9+
# Medium (55.15%)
10+
# Likes: 1374
11+
# Dislikes: 0
12+
# Total Accepted: 308.7K
13+
# Total Submissions: 559.8K
14+
# Testcase Example: '[100,4,200,1,3,2]'
15+
#
16+
# 给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。
17+
#
18+
# 请你设计并实现时间复杂度为 O(n) 的算法解决此问题。
19+
#
20+
#
21+
#
22+
# 示例 1:
23+
#
24+
#
25+
# 输入:nums = [100,4,200,1,3,2]
26+
# 输出:4
27+
# 解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
28+
#
29+
# 示例 2:
30+
#
31+
#
32+
# 输入:nums = [0,3,7,2,5,8,4,6,0,1]
33+
# 输出:9
34+
#
35+
#
36+
#
37+
#
38+
# 提示:
39+
#
40+
#
41+
# 0
42+
# -10^9
43+
#
44+
#
45+
#
46+
47+
# @lc code=start
48+
class Solution:
49+
def longestConsecutive(self, nums: list[int]) -> int:
50+
num_set = set(nums)
51+
result = 0
52+
53+
while num_set:
54+
n = num_set.pop()
55+
curr = 1
56+
57+
x = n - 1
58+
while x in num_set:
59+
curr += 1
60+
num_set.remove(x)
61+
x = x - 1
62+
63+
x = n + 1
64+
while x in num_set:
65+
curr += 1
66+
num_set.remove(x)
67+
x = x + 1
68+
69+
result = max(result, curr)
70+
71+
return result
72+
# @lc code=end

133.克隆图.ts

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* @lc app=leetcode.cn id=133 lang=typescript
3+
*
4+
* [133] 克隆图
5+
*
6+
* https://leetcode.cn/problems/clone-graph/description/
7+
*
8+
* algorithms
9+
* Medium (68.53%)
10+
* Likes: 530
11+
* Dislikes: 0
12+
* Total Accepted: 99.8K
13+
* Total Submissions: 145.5K
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+
class Node {
90+
val: number
91+
92+
neighbors: Node[]
93+
94+
constructor(val?: number, neighbors?: Node[]) {
95+
this.val = (val === undefined ? 0 : val)
96+
this.neighbors = (neighbors === undefined ? [] : neighbors)
97+
}
98+
}
99+
100+
export
101+
// @lc code=start
102+
function cloneGraph(node: Node | null): Node | null {
103+
if (!node) return null
104+
105+
const stack = [node]
106+
const mapping = new Map<Node, Node>()
107+
const clonedSet = new Set(stack)
108+
109+
const getOrCreate = (original: Node) => {
110+
if (!mapping.has(original)) {
111+
const c = new Node(original.val)
112+
mapping.set(original, c)
113+
}
114+
return mapping.get(original)!
115+
}
116+
117+
while (stack.length) {
118+
const original = stack.pop()!
119+
const cloned = getOrCreate(original)
120+
121+
for (const n of original.neighbors) {
122+
cloned.neighbors.push(getOrCreate(n))
123+
124+
if (!clonedSet.has(n)) {
125+
clonedSet.add(n)
126+
stack.push(n)
127+
}
128+
}
129+
}
130+
131+
return mapping.get(node)!
132+
}
133+
// @lc code=end

0 commit comments

Comments
 (0)