Skip to content

Commit 1bf7996

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

12 files changed

+478
-29
lines changed

15.三数之和.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,27 @@
5555
export
5656
// @lc code=start
5757
function threeSum(nums: number[]): [number, number, number][] {
58-
if (nums.length < 3) return []
59-
60-
const result: Array<[number, number, number]> = []
61-
nums.sort((a, b) => a - b) // [-1,-1,-4,0,1,2]
58+
const result: [number, number, number][] = []
59+
nums.sort((a, b) => a - b)
6260

6361
for (let i = 0; i < nums.length - 2; i++) {
6462
if (i > 0 && nums[i] === nums[i - 1]) {
6563
continue
6664
}
6765

6866
let k = nums.length - 1
67+
6968
for (let j = i + 1; j < nums.length - 1; j++) {
7069
if (j > i + 1 && nums[j] === nums[j - 1]) {
7170
continue
7271
}
7372

7473
while (j < k && nums[i] + nums[j] + nums[k] > 0) {
75-
k -= 1
74+
k--
75+
}
76+
if (j >= k) {
77+
break
7678
}
77-
if (j === k) break
7879

7980
if (nums[i] + nums[j] + nums[k] === 0) {
8081
result.push([nums[i], nums[j], nums[k]])
@@ -85,3 +86,34 @@ function threeSum(nums: number[]): [number, number, number][] {
8586
return result
8687
}
8788
// @lc code=end
89+
90+
// function threeSum(nums: number[]): [number, number, number][] {
91+
// if (nums.length < 3) return []
92+
93+
// const result: Array<[number, number, number]> = []
94+
// nums.sort((a, b) => a - b) // [-1,-1,-4,0,1,2]
95+
96+
// for (let i = 0; i < nums.length - 2; i++) {
97+
// if (i > 0 && nums[i] === nums[i - 1]) {
98+
// continue
99+
// }
100+
101+
// let k = nums.length - 1
102+
// for (let j = i + 1; j < nums.length - 1; j++) {
103+
// if (j > i + 1 && nums[j] === nums[j - 1]) {
104+
// continue
105+
// }
106+
107+
// while (j < k && nums[i] + nums[j] + nums[k] > 0) {
108+
// k -= 1
109+
// }
110+
// if (j === k) break
111+
112+
// if (nums[i] + nums[j] + nums[k] === 0) {
113+
// result.push([nums[i], nums[j], nums[k]])
114+
// }
115+
// }
116+
// }
117+
118+
// return result
119+
// }

16.最接近的三数之和.ts

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,57 @@ export
5252
// @lc code=start
5353
function threeSumClosest(nums: number[], target: number): number {
5454
nums.sort((a, b) => a - b)
55-
let result = Infinity
55+
let result = Number.MAX_SAFE_INTEGER
5656

5757
for (let i = 0; i < nums.length - 2; i++) {
58-
let j = i + 1; let
59-
k = nums.length - 1
58+
let k = nums.length - 1
6059

61-
while (j < k) {
62-
const sum = nums[i] + nums[j] + nums[k]
63-
if (sum === target) return sum
64-
if (Math.abs(sum - target) < Math.abs(result - target)) {
65-
result = sum
66-
}
67-
68-
if (sum > target) {
60+
for (let j = i + 1; j < nums.length - 1; j++) {
61+
while (j < k && nums[i] + nums[j] + nums[k] > target) {
62+
result = closest(target, nums[i] + nums[j] + nums[k], result)
6963
k -= 1
70-
} else {
71-
j += 1
7264
}
65+
if (j === k) break
66+
67+
result = closest(target, nums[i] + nums[j] + nums[k], result)
7368
}
7469
}
7570

7671
return result
7772
}
73+
74+
function closest(target: number, a: number, b: number) {
75+
const absA = Math.abs(a - target), absB = Math.abs(b - target)
76+
77+
if (absA < absB) {
78+
return a
79+
}
80+
return b
81+
}
7882
// @lc code=end
83+
84+
// function threeSumClosest(nums: number[], target: number): number {
85+
// nums.sort((a, b) => a - b)
86+
// let result = Infinity
87+
88+
// for (let i = 0; i < nums.length - 2; i++) {
89+
// let j = i + 1; let
90+
// k = nums.length - 1
91+
92+
// while (j < k) {
93+
// const sum = nums[i] + nums[j] + nums[k]
94+
// if (sum === target) return sum
95+
// if (Math.abs(sum - target) < Math.abs(result - target)) {
96+
// result = sum
97+
// }
98+
99+
// if (sum > target) {
100+
// k -= 1
101+
// } else {
102+
// j += 1
103+
// }
104+
// }
105+
// }
106+
107+
// return result
108+
// }

18.四数之和.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* @lc app=leetcode.cn id=18 lang=typescript
3+
*
4+
* [18] 四数之和
5+
*
6+
* https://leetcode-cn.com/problems/4sum/description/
7+
*
8+
* algorithms
9+
* Medium (39.45%)
10+
* Likes: 1134
11+
* Dislikes: 0
12+
* Total Accepted: 271.9K
13+
* Total Submissions: 689.1K
14+
* Testcase Example: '[1,0,-1,0,-2,2]\n0'
15+
*
16+
* 给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a],
17+
* nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复):
18+
*
19+
*
20+
* 0 <= a, b, c, d < n
21+
* a、b、c 和 d 互不相同
22+
* nums[a] + nums[b] + nums[c] + nums[d] == target
23+
*
24+
*
25+
* 你可以按 任意顺序 返回答案 。
26+
*
27+
*
28+
*
29+
* 示例 1:
30+
*
31+
*
32+
* 输入:nums = [1,0,-1,0,-2,2], target = 0
33+
* 输出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
34+
*
35+
*
36+
* 示例 2:
37+
*
38+
*
39+
* 输入:nums = [2,2,2,2,2], target = 8
40+
* 输出:[[2,2,2,2]]
41+
*
42+
*
43+
*
44+
*
45+
* 提示:
46+
*
47+
*
48+
* 1 <= nums.length <= 200
49+
* -10^9 <= nums[i] <= 10^9
50+
* -10^9 <= target <= 10^9
51+
*
52+
*
53+
*/
54+
55+
export
56+
// @lc code=start
57+
function fourSum(nums: number[], target: number): number[][] {
58+
nums.sort((a, b) => a - b)
59+
const result: number[][] = []
60+
61+
for (let a = 0; a < nums.length - 3; a++) {
62+
if (a > 0 && nums[a - 1] === nums[a]) {
63+
continue
64+
}
65+
66+
for (let b = a + 1; b < nums.length - 2; b++) {
67+
if (b > a + 1 && nums[b] === nums[b - 1]) {
68+
continue
69+
}
70+
71+
const sumAB = nums[a] + nums[b]
72+
73+
let d = nums.length - 1
74+
for (let c = b + 1; c < nums.length - 1; c++) {
75+
if (c > b + 1 && nums[c] === nums[c - 1]) {
76+
continue
77+
}
78+
while (c < d && sumAB + nums[c] + nums[d] > target) {
79+
d--
80+
}
81+
if (c >= d) break
82+
83+
if (sumAB + nums[c] + nums[d] === target) {
84+
result.push([nums[a], nums[b], nums[c], nums[d]])
85+
}
86+
}
87+
}
88+
}
89+
90+
return result
91+
}
92+
// @lc code=end

25.k-个一组翻转链表.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* @lc app=leetcode.cn id=25 lang=typescript
3+
*
4+
* [25] K 个一组翻转链表
5+
*
6+
* https://leetcode-cn.com/problems/reverse-nodes-in-k-group/description/
7+
*
8+
* algorithms
9+
* Hard (66.13%)
10+
* Likes: 1485
11+
* Dislikes: 0
12+
* Total Accepted: 279.3K
13+
* Total Submissions: 422.4K
14+
* Testcase Example: '[1,2,3,4,5]\n2'
15+
*
16+
* 给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。
17+
*
18+
* k 是一个正整数,它的值小于或等于链表的长度。
19+
*
20+
* 如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。
21+
*
22+
* 进阶:
23+
*
24+
*
25+
* 你可以设计一个只使用常数额外空间的算法来解决此问题吗?
26+
* 你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。
27+
*
28+
*
29+
*
30+
*
31+
* 示例 1:
32+
*
33+
*
34+
* 输入:head = [1,2,3,4,5], k = 2
35+
* 输出:[2,1,4,3,5]
36+
*
37+
*
38+
* 示例 2:
39+
*
40+
*
41+
* 输入:head = [1,2,3,4,5], k = 3
42+
* 输出:[3,2,1,4,5]
43+
*
44+
*
45+
* 示例 3:
46+
*
47+
*
48+
* 输入:head = [1,2,3,4,5], k = 1
49+
* 输出:[1,2,3,4,5]
50+
*
51+
*
52+
* 示例 4:
53+
*
54+
*
55+
* 输入:head = [1], k = 1
56+
* 输出:[1]
57+
*
58+
*
59+
*
60+
*
61+
*
62+
* 提示:
63+
*
64+
*
65+
* 列表中节点的数量在范围 sz 内
66+
* 1
67+
* 0
68+
* 1
69+
*
70+
*
71+
*/
72+
73+
import { ListNode } from './commons/list'
74+
75+
export
76+
// @lc code=start
77+
function reverseKGroup(head: ListNode | null, k: number): ListNode | null {
78+
const dummy = new ListNode(-1, head)
79+
let prevEnd = dummy
80+
81+
let findResult = findNextK(prevEnd, k)
82+
while (findResult) {
83+
const [currTail, nextBegin] = findResult
84+
const currHead = prevEnd.next!
85+
reverse(currHead, currTail)
86+
prevEnd.next = currTail
87+
currHead.next = nextBegin
88+
prevEnd = currHead
89+
findResult = findNextK(prevEnd, k)
90+
}
91+
92+
return dummy.next
93+
}
94+
95+
function findNextK(prevEnd: ListNode, k: number): [ListNode, ListNode | null] | null {
96+
let curr: ListNode = prevEnd, next: ListNode | null = prevEnd.next
97+
98+
while (k) {
99+
if (!curr.next) return null
100+
101+
curr = curr.next
102+
next = curr.next
103+
k -= 1
104+
}
105+
106+
return [curr, next]
107+
}
108+
109+
function reverse(begin: ListNode, end: ListNode) {
110+
let prev: ListNode | null = null, curr: ListNode = begin
111+
112+
while (prev !== end) {
113+
const next = curr.next
114+
curr.next = prev
115+
116+
prev = curr
117+
curr = next!
118+
}
119+
}
120+
// @lc code=end
121+
122+
/*
123+
124+
.
125+
-1, [1, 2, 3, 4]
126+
127+
prevEnd = -1
128+
129+
prev = 1
130+
curr = 2
131+
next = 3
132+
133+
*/
134+
135+
// -1, [1, 2]

0 commit comments

Comments
 (0)