Skip to content

Commit 6a9d7ae

Browse files
committed
feat: ^_^
1 parent cf96140 commit 6a9d7ae

File tree

50 files changed

+388
-0
lines changed

Some content is hidden

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

50 files changed

+388
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* @lc app=leetcode.cn id=105 lang=typescript
3+
*
4+
* [105] 从前序与中序遍历序列构造二叉树
5+
*
6+
* https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/
7+
*
8+
* algorithms
9+
* Medium (70.72%)
10+
* Likes: 1385
11+
* Dislikes: 0
12+
* Total Accepted: 288.1K
13+
* Total Submissions: 407.3K
14+
* Testcase Example: '[3,9,20,15,7]\n[9,3,15,20,7]'
15+
*
16+
* 给定一棵树的前序遍历 preorder 与中序遍历  inorder。请构造二叉树并返回其根节点。
17+
*
18+
*
19+
*
20+
* 示例 1:
21+
*
22+
*
23+
* Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
24+
* Output: [3,9,20,null,null,15,7]
25+
*
26+
*
27+
* 示例 2:
28+
*
29+
*
30+
* Input: preorder = [-1], inorder = [-1]
31+
* Output: [-1]
32+
*
33+
*
34+
*
35+
*
36+
* 提示:
37+
*
38+
*
39+
* 1
40+
* inorder.length == preorder.length
41+
* -3000
42+
* preorder 和 inorder 均无重复元素
43+
* inorder 均出现在 preorder
44+
* preorder 保证为二叉树的前序遍历序列
45+
* inorder 保证为二叉树的中序遍历序列
46+
*
47+
*
48+
*/
49+
50+
import { TreeNode } from './commons/Tree'
51+
52+
export
53+
// @lc code=start
54+
function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
55+
function helper(preBegin: number, preEnd: number, inBegin: number, inEnd: number): TreeNode|null {
56+
if (preEnd < preBegin) {
57+
return null
58+
}
59+
60+
const root = new TreeNode(preorder[preBegin])
61+
if (preBegin === preEnd) {
62+
return root
63+
}
64+
65+
const rootVal = preorder[preBegin]
66+
const rootInOrderIndex = inorder.indexOf(rootVal)
67+
68+
const leftInOrderBegin = inBegin
69+
const leftInOrderEnd = rootInOrderIndex - 1
70+
const rightInOrderBegin = rootInOrderIndex + 1
71+
const rightInOrderEnd = inEnd
72+
73+
const leftLength = leftInOrderEnd - leftInOrderBegin
74+
75+
const leftPreOrderBegin = preBegin + 1
76+
const leftPreOrderEnd = leftPreOrderBegin + leftLength
77+
const rightPreOrderBegin = leftPreOrderEnd + 1
78+
const rightPreOrderEnd = preEnd
79+
80+
root.left = helper(leftPreOrderBegin, leftPreOrderEnd, leftInOrderBegin, leftInOrderEnd)
81+
root.right = helper(rightPreOrderBegin, rightPreOrderEnd, rightInOrderBegin, rightInOrderEnd)
82+
return root
83+
}
84+
85+
const result = helper(0, preorder.length - 1, 0, inorder.length - 1)
86+
return result
87+
}
88+
// @lc code=end

11.盛最多水的容器.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* @lc app=leetcode.cn id=11 lang=typescript
3+
*
4+
* [11] 盛最多水的容器
5+
*
6+
* https://leetcode-cn.com/problems/container-with-most-water/description/
7+
*
8+
* algorithms
9+
* Medium (62.12%)
10+
* Likes: 3117
11+
* Dislikes: 0
12+
* Total Accepted: 609.6K
13+
* Total Submissions: 981.5K
14+
* Testcase Example: '[1,8,6,2,5,4,8,3,7]'
15+
*
16+
* 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为
17+
* (i, ai) 和 (i, 0) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
18+
*
19+
* 说明:你不能倾斜容器。
20+
*
21+
*
22+
*
23+
* 示例 1:
24+
*
25+
*
26+
*
27+
*
28+
* 输入:[1,8,6,2,5,4,8,3,7]
29+
* 输出:49
30+
* 解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。
31+
*
32+
* 示例 2:
33+
*
34+
*
35+
* 输入:height = [1,1]
36+
* 输出:1
37+
*
38+
*
39+
* 示例 3:
40+
*
41+
*
42+
* 输入:height = [4,3,2,1,4]
43+
* 输出:16
44+
*
45+
*
46+
* 示例 4:
47+
*
48+
*
49+
* 输入:height = [1,2,1]
50+
* 输出:2
51+
*
52+
*
53+
*
54+
*
55+
* 提示:
56+
*
57+
*
58+
* n == height.length
59+
* 2 <= n <= 10^5
60+
* 0 <= height[i] <= 10^4
61+
*
62+
*
63+
*/
64+
65+
export
66+
// @lc code=start
67+
function maxArea(height: number[]): number {
68+
let left = 0
69+
let right = height.length - 1
70+
let result = 0
71+
72+
while (left < right) {
73+
result = Math.max(Math.min(height[left], height[right]) * (right - left), result)
74+
75+
if (height[left] > height[right]) {
76+
right -= 1
77+
} else {
78+
left += 1
79+
}
80+
}
81+
82+
return result
83+
}
84+
// @lc code=end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* @lc app=leetcode.cn id=238 lang=typescript
3+
*
4+
* [238] 除自身以外数组的乘积
5+
*
6+
* https://leetcode-cn.com/problems/product-of-array-except-self/description/
7+
*
8+
* algorithms
9+
* Medium (72.32%)
10+
* Likes: 942
11+
* Dislikes: 0
12+
* Total Accepted: 141.5K
13+
* Total Submissions: 195.5K
14+
* Testcase Example: '[1,2,3,4]'
15+
*
16+
* 给你一个长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i]
17+
* 之外其余各元素的乘积。
18+
*
19+
*
20+
*
21+
* 示例:
22+
*
23+
* 输入: [1,2,3,4]
24+
* 输出: [24,12,8,6]
25+
*
26+
*
27+
*
28+
* 提示:题目数据保证数组之中任意元素的全部前缀元素和后缀(甚至是整个数组)的乘积都在 32 位整数范围内。
29+
*
30+
* 说明: 请不要使用除法,且在 O(n) 时间复杂度内完成此题。
31+
*
32+
* 进阶:
33+
* 你可以在常数空间复杂度内完成这个题目吗?( 出于对空间复杂度分析的目的,输出数组不被视为额外空间。)
34+
*
35+
*/
36+
37+
export
38+
// @lc code=start
39+
function productExceptSelf(nums: number[]): number[] {
40+
let prev = 1
41+
const result = nums.map((x) => {
42+
prev *= x
43+
return prev
44+
})
45+
46+
prev = 1
47+
for (let i = nums.length - 1; i >= 0; i--) {
48+
if (i >= 1) {
49+
result[i] = result[i - 1] * prev
50+
} else {
51+
result[i] = prev
52+
}
53+
prev *= nums[i]
54+
}
55+
56+
return result
57+
}
58+
// @lc code=end

240.搜索二维矩阵-ii.ts

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=240 lang=typescript
3+
*
4+
* [240] 搜索二维矩阵 II
5+
*
6+
* https://leetcode-cn.com/problems/search-a-2d-matrix-ii/description/
7+
*
8+
* algorithms
9+
* Medium (49.92%)
10+
* Likes: 910
11+
* Dislikes: 0
12+
* Total Accepted: 227.3K
13+
* Total Submissions: 454.9K
14+
* Testcase Example: '[[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]]\n' +
15+
'5'
16+
*
17+
* 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性:
18+
*
19+
*
20+
* 每行的元素从左到右升序排列。
21+
* 每列的元素从上到下升序排列。
22+
*
23+
*
24+
*
25+
*
26+
* 示例 1:
27+
*
28+
*
29+
* 输入:matrix =
30+
* [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]],
31+
* target = 5
32+
* 输出:true
33+
*
34+
*
35+
* 示例 2:
36+
*
37+
*
38+
* 输入:matrix =
39+
* [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]],
40+
* target = 20
41+
* 输出:false
42+
*
43+
*
44+
*
45+
*
46+
* 提示:
47+
*
48+
*
49+
* m == matrix.length
50+
* n == matrix[i].length
51+
* 1 <= n, m <= 300
52+
* -10^9 <= matrix[i][j] <= 10^9
53+
* 每行的所有元素从左到右升序排列
54+
* 每列的所有元素从上到下升序排列
55+
* -10^9 <= target <= 10^9
56+
*
57+
*
58+
*/
59+
export
60+
// @lc code=start
61+
function searchMatrix(matrix: number[][], target: number): boolean {
62+
let i = matrix.length - 1
63+
let j = 0
64+
65+
while (i >= 0 && j < matrix[0].length) {
66+
if (matrix[i][j] === target) {
67+
return true
68+
}
69+
if (matrix[i][j] > target) {
70+
i -= 1
71+
} else {
72+
j += 1
73+
}
74+
}
75+
76+
return false
77+
}
78+
// @lc code=end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { buildTree } from '../105.从前序与中序遍历序列构造二叉树'
2+
import { TreeNode } from '../commons/Tree'
3+
4+
describe('105.从前序与中序遍历序列构造二叉树', () => {
5+
it('should work', () => {
6+
expect(buildTree([3, 9, 20, 15, 7], [9, 3, 15, 20, 7])?.toString())
7+
.toBe(TreeNode.fromArray([3, 9, 20, null, null, 15, 7])?.toString())
8+
expect(buildTree([-1], [-1])?.toString())
9+
.toBe(TreeNode.fromArray([-1])?.toString())
10+
})
11+
})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { maxArea } from '../11.盛最多水的容器'
2+
3+
describe('11.盛最多水的容器', () => {
4+
it('should work', () => {
5+
expect(maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7])).toBe(49)
6+
expect(maxArea([1, 1])).toBe(1)
7+
})
8+
})

0 commit comments

Comments
 (0)