We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent deefbd6 commit 1fd3e9bCopy full SHA for 1fd3e9b
3sum/hyer0705.ts
@@ -0,0 +1,31 @@
1
+function threeSum(nums: number[]): number[][] {
2
+ nums.sort((a, b) => a - b);
3
+
4
+ const result: number[][] = [];
5
+ for (let i = 0; i < nums.length - 2; i++) {
6
+ if (i > 0 && nums[i] === nums[i - 1]) continue;
7
+ const target = -nums[i];
8
9
+ let j = i + 1;
10
+ let k = nums.length - 1;
11
+ while (j < k) {
12
+ const currentSum = nums[j] + nums[k];
13
14
+ if (target < currentSum) {
15
+ k--;
16
+ } else if (target > currentSum) {
17
+ j++;
18
+ } else {
19
+ result.push([nums[i], nums[j], nums[k]]);
20
21
22
23
24
+ while (j < k && nums[j] === nums[j - 1]) j++;
25
+ while (j < k && nums[k] === nums[k + 1]) k--;
26
+ }
27
28
29
30
+ return result;
31
+}
0 commit comments