Skip to content

Commit d2e5e70

Browse files
committed
Week 2
- 3sum
1 parent e873d7e commit d2e5e70

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

3sum/jiji-hoon96.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// time limit 실패
2+
3+
// function threeSum(nums: number[]): number[][] {
4+
// const result: number[][] = [];
5+
// for (let i = 0; i < nums.length; i++) {
6+
// for (let j = 1; j < nums.length; j++) {
7+
// for (let k = 2; k < nums.length; k++) {
8+
// if (
9+
// i !== j &&
10+
// j !== k &&
11+
// i !== k &&
12+
// nums[i] + nums[j] + nums[k] === 0
13+
// ) {
14+
// const hasArray = result.some(
15+
// (item) =>
16+
// JSON.stringify([...item].sort((a, b) => a - b)) ===
17+
// JSON.stringify([nums[i], nums[j], nums[k]].sort((a, b) => a - b)),
18+
// );
19+
// if (!hasArray) {
20+
// result.push([nums[i], nums[j], nums[k]]);
21+
// }
22+
// }
23+
// }
24+
// }
25+
// }
26+
// return result;
27+
// }
28+
29+
function threeSum(nums: number[]): number[][] {
30+
const result: number[][] = [];
31+
nums.sort((a, b) => a - b);
32+
33+
for (let i = 0; i < nums.length - 2; i++) {
34+
if (i > 0 && nums[i] === nums[i - 1]) continue;
35+
36+
let left = i + 1;
37+
let right = nums.length - 1;
38+
39+
while (left < right) {
40+
const sum = nums[i] + nums[left] + nums[right];
41+
42+
if (sum === 0) {
43+
result.push([nums[i], nums[left], nums[right]]);
44+
while (left < right && nums[left] === nums[left + 1]) left++;
45+
while (left < right && nums[right] === nums[right - 1]) right--;
46+
left++;
47+
right--;
48+
} else if (sum < 0) {
49+
left++;
50+
} else {
51+
right--;
52+
}
53+
}
54+
}
55+
return result;
56+
}
57+
58+
threeSum([-1, 0, 1, 2, -1, -4]); // [[-1,-1,2],[-1,0,1]]
59+
threeSum([0, 1, 1]); // []
60+
threeSum([0, 0, 0]); // [[0,0,0]]
61+
threeSum([
62+
12, 5, -12, 4, -11, 11, 2, 7, 2, -5, -14, -3, -3, 3, 2, -10, 9, -15, 2, 14,
63+
-3, -15, -3, -14, -1, -7, 11, -4, -11, 12, -15, -14, 2, 10, -2, -1, 6, 7, 13,
64+
-15, -13, 6, -10, -9, -14, 7, -12, 3, -1, 5, 2, 11, 6, 14, 12, -10, 14, 0, -7,
65+
11, -10, -7, 4, -1, -12, -13, 13, 1, 9, 3, 1, 3, -5, 6, 9, -4, -2, 5, 14, 12,
66+
-5, -6, 1, 8, -15, -10, 5, -15, -2, 5, 3, 3, 13, -8, -13, 8, -5, 8, -6, 11,
67+
-12, 3, 0, -2, -6, -14, 2, 0, 6, 1, -11, 9, 2, -3, -6, 3, 3, -15, -5, -14, 5,
68+
13, -4, -4, -10, -10, 11,
69+
]); // [[-15,1,14],[-15,2,13],[-15,3,12],[-15,4,11],[-15,5,10],[-15,6,9],[-15,7,8],[-14,0,14],[-14,1,13],[-14,2,12],[-14,3,11],[-14,4,10],[-14,5,9],[-14,6,8],[-14,7,7],[-13,-1,14],[-13,0,13],[-13,1,12],[-13,2,11],[-13,3,10],[-13,4,9],[-13,5,8],[-13,6,7],[-12,-2,14],[-12,-1,13],[-12,0,12],[-12,1,11],[-12,2,10],[-12,3,9],[-12,4,8],[-12,5,7],[-12,6,6],[-11,-3,14],[-11,-2,13],[-11,-1,12],[-11,0,11],[-11,1,10],[-11,2,9],[-11,3,8],[-11,4,7],[-11,5,6],[-10,-4,14],[-10,-3,13],[-10,-2,12],[-10,-1,11],[-10,0,10],[-10,1,9],[-10,2,8],[-10,3,7],[-10,4,6],[-10,5,5],[-9,-5,14],[-9,-4,13],[-9,-3,12],[-9,-2,11],[-9,-1,10],[-9,0,9],[-9,1,8],[-9,2,7],[-9,3,6],[-9,4,5],[-8,-6,14],[-8,-5,13],[-8,-4,12],[-8,-3,11],[-8,-2,10],[-8,-1,9],[-8,0,8],[-8,1,7],[-8,2,6],[-8,3,5],[-8,4,4],[-7,-7,14],[-7,-6,13],[-7,-5,12],[-7,-4,11],[-7,-3,10],[-7,-2,9],[-7,-1,8],[-7,0,7],[-7,1,6],[-7,2,5],[-7,3,4],[-6,-6,12],[-6,-5,11],[-6,-4,10],[-6,-3,9],[-6,-2,8],[-6,-1,7],[-6,0,6],[-6,1,5],[-6,2,4],[-6,3,3],[-5,-5,10],[-5,-4,9],[-5,-3,8...

0 commit comments

Comments
 (0)