deno.land / x / masx200_leetcode_test@10.6.5 / 3sum / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
export default function threeSum(nums: number[]): number[][] { if (nums.length < 3) { return []; }
nums.sort((a, b) => a - b); const cache = new Set<string>();
const ans: number[][] = [];
const len = nums.length;
for (let i = 0; i < len; i++) { if (nums[i] > 0) break; // 如果当前数字大于0,则三数之和一定大于0,所以结束循环
let L = i + 1; let R = len - 1; while (L < R) { const sum = nums[i] + nums[L] + nums[R]; if (sum == 0) { const a = [nums[i], nums[L], nums[R]]; const key = a.join(""); if (!cache.has(key)) { cache.add(key); ans.push(a); }
L++; R--; } else if (sum < 0) L++; else if (sum > 0) R--; } }
return ans;}
masx200_leetcode_test
leetcode题解和一些测试工具
DocumentationDocumentation
GitHub Repositorymasx200/leetcode-test
GitHub Stars
4

Version Info

Tagged at
4 months ago