8000 15th problem · Tahirc1/LeetcodeJs@f5d28a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit f5d28a8

Browse files
committed
15th problem
1 parent effe5a3 commit f5d28a8

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

15. 3Sum.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var threeSum = function(nums) {
2+
let a = []
3+
let [j,k,val1,val2] = [0,0,0,0]
4+
nums.sort(function(a,b){return a-b})
5+
for(let i = 0 ;i < nums.length-2;i++){
6+
if(nums[i] > 0 ){continue}
7+
if(nums[i] == nums[i-1]){continue}
8+
j = 1 + i
9+
k = nums.length - 1
10+
while(j < k ){
11+
if(nums[i] + nums[j] + nums[k] == 0){
12+
a.push([nums[i] , nums[j] , nums[k]])
13+
val1=nums[j];
14+
while(j<k && nums[j]==val1) j++;
15+
val2=nums[k];
16+
while(j<k && nums[k]==val2) k--;
17+
}else if(nums[i]+nums[j]+nums[k]<0) j++;
18+
else if(nums[i]+nums[j]+nums[k]>0) k--;
19+
}
20+
}
21+
return a
22+
};

0 commit comments

Comments
 (0)
0