Skip to content

Commit 8bbef62

Browse files
committed
add: 3Sum
1 parent 8811db6 commit 8bbef62

File tree

5 files changed

+156
-100
lines changed

5 files changed

+156
-100
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
1616
|11|[Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [JavaScript](./src/container-with-most-water/res.js)|Medium|
1717
|13|[Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [JavaScript](./src/roman-to-integer/res.js)|Easy|
1818
|14|[Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/) | [JavaScript](./src/longest-common-prefix/res.js)|Easy|
19+
|15|[3Sum](https://leetcode.com/problems/3sum/) | [JavaScript](./src/3sum/res.js) |Medium|
1920
|17|[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | [JavaScript](./src/letter-combinations-of-a-phone-number/res.js)|Medium|
2021
|19|[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) | [JavaScript](./src/remove-nth-node-from-end-of-list/res.js)|Medium|
2122
|22|[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) | [JavaScript](./src/generate-parentheses/res.js)|Medium|

src/3sum/res.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* res.js
3+
* @authors Joe Jiang ([email protected])
4+
* @date 2017-04-03 23:45:47
5+
*
6+
* @param {number[]} nums
7+
* @return {number[][]}
8+
*/
9+
let threeSum = function(nums) {
10+
nums.sort(function(a, b) {
11+
return a - b;
12+
});
13+
14+
let numlen = nums.length,
15+
end = nums[numlen - 1],
16+
eInd = numlen - 2,
17+
res = [];
18+
19+
if (numlen < 3 || nums[0] > 0 || end < 0) {
20+
return [];
21+
}
22+
23+
for (let i = 0; i < eInd; i++) {
24+
let revi = -nums[i],
25+
j = i + 1,
26+
k = numlen - 1;
27+
28+
if (revi < 0) {
29+
break;
30+
}
31+
32+
while (j < k) {
33+
let norj = nums[j],
34+
nork = nums[k];
35+
if (norj + nork > revi) {
36+
k--;
37+
} else if (norj + nork < revi) {
38+
j++;
39+
} else {
40+
res.push([nums[i], nums[j], nums[k]]);
41+
42+
// forbid duplicated numbers
43+
while (j + 1 < numlen && nums[j] === nums[j + 1]) j++;
44+
while (nums[k] === nums[k - 1]) k--;
45+
k--;
46+
j++;
47+
48+
}
49+
}
50+
51+
while (i + 1 < numlen && nums[i] === nums[i + 1]) i++;
52+
}
53+
54+
return res;
55+
};

src/longest-common-prefix/res.js

+27-27
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,35 @@
1010
* @return {string}
1111
*/
1212
let longestCommonPrefix = function(strs) {
13-
if (strs === '' || strs.length === 0) {
14-
return '';
15-
}
16-
17-
let prefix = '',
18-
strslen = strs.length,
19-
strlen = strs[0].length;
20-
21-
for (let i=0; i < strlen; i++) {
22-
let jud = true;
23-
24-
for (let j=1; j<strslen; j++) {
25-
if (strs[j][i] === undefined || strs[j][i] !== strs[0][i]) {
26-
jud = false;
27-
break;
28-
}
29-
}
30-
31-
if (jud) {
32-
prefix += strs[0][i];
33-
} else {
34-
return prefix;
35-
}
36-
}
37-
38-
return prefix;
13+
if (strs === '' || strs.length === 0) {
14+
return '';
15+
}
16+
17+
let prefix = '',
18+
strslen = strs.length,
19+
strlen = strs[0].length;
20+
21+
for (let i = 0; i < strlen; i++) {
22+
let jud = true;
23+
24+
for (let j = 1; j < strslen; j++) {
25+
if (strs[j][i] === undefined || strs[j][i] !== strs[0][i]) {
26+
jud = false;
27+
break;
28+
}
29+
}
30+
31+
if (jud) {
32+
prefix += strs[0][i];
33+
} else {
34+
return prefix;
35+
}
36+
}
37+
38+
return prefix;
3939
};
4040

4141
/**
4242
* Another solution: Sort the array first, and then you can simply compare the first and last elements in the sorted array.
4343
*
44-
*/
44+
*/

src/permutations/res.js

+33-33
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,39 @@
99
* @return {number[][]}
1010
*/
1111
let permute = function(nums) {
12-
let numlen = nums.length,
13-
res = [];
14-
15-
if (numlen === 0) {
16-
return res;
17-
} else if (numlen === 1) {
18-
res.push(nums);
19-
return res;
20-
}
21-
22-
return subpermute([], nums);
12+
let numlen = nums.length,
13+
res = [];
14+
15+
if (numlen === 0) {
16+
return res;
17+
} else if (numlen === 1) {
18+
res.push(nums);
19+
return res;
20+
}
21+
22+
return subpermute([], nums);
2323
};
2424

2525
let subpermute = function(base, nums) {
26-
let numlen = nums.length,
27-
res = [];
28-
29-
if (numlen === 1) {
30-
return [ base.concat(nums) ];
31-
}
32-
33-
for (let i=0; i<numlen; i++) {
34-
let subarray = [];
35-
for (let j=0; j<numlen; j++) {
36-
if (i===j) {
37-
continue;
38-
}
39-
subarray.push(nums[j]);
40-
}
41-
42-
let newbase = base.concat( [nums[i]] );
43-
res = res.concat( subpermute(newbase, subarray) );
44-
}
45-
46-
return res;
47-
};
26+
let numlen = nums.length,
27+
res = [];
28+
29+
if (numlen === 1) {
30+
return [base.concat(nums)];
31+
}
32+
33+
for (let i = 0; i < numlen; i++) {
34+
let subarray = [];
35+
for (let j = 0; j < numlen; j++) {
36+
if (i === j) {
37+
continue;
38+
}
39+
subarray.push(nums[j]);
40+
}
41+
42+
let newbase = base.concat([nums[i]]);
43+
res = res.concat(subpermute(newbase, subarray));
44+
}
45+
46+
return res;
47+
};

src/string-to-integer-atoi/res.js

+40-40
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,43 @@
99
* @return {number}
1010
*/
1111
let myAtoi = function(str) {
12-
const INT_MAX = 2147483647,
13-
INT_MIN = -2147483648,
14-
bound = Number.parseInt(INT_MAX/10);
15-
let strlen = str.length,
16-
signal = 1, // 1 stands for positive number, 0 stands for negative number
17-
res = 0;
18-
19-
while (str[0] && str[0] === ' ') {
20-
str = str.slice(1);
21-
strlen -= 1;
22-
}
23-
24-
if (strlen === 0) {
25-
return 0;
26-
}
27-
28-
if(str[0] === '-') {
29-
signal = 0;
30-
str = str.slice(1);
31-
} else if (str[0] === "+") {
32-
str = str.slice(1);
33-
}
34-
35-
while (str[0] >= '0' && str[0] <= '9') {
36-
// console.log(str[0]);
37-
let element = Number.parseInt(str[0]);
38-
39-
if ( res>bound || (res===bound && element>7) ) {
40-
if(signal) {
41-
return INT_MAX;
42-
}
43-
return INT_MIN;
44-
}
45-
46-
res = res * 10 + element;
47-
str = str.slice(1);
48-
}
49-
50-
return signal? res:-res;
51-
};
12+
const INT_MAX = 2147483647,
13+
INT_MIN = -2147483648,
14+
bound = Number.parseInt(INT_MAX / 10);
15+
let strlen = str.length,
16+
signal = 1, // 1 stands for positive number, 0 stands for negative number
17+
res = 0;
18+
19+
while (str[0] && str[0] === ' ') {
20+
str = str.slice(1);
21+
strlen -= 1;
22+
}
23+
24+
if (strlen === 0) {
25+
return 0;
26+
}
27+
28+
if (str[0] === '-') {
29+
signal = 0;
30+
str = str.slice(1);
31+
} else if (str[0] === "+") {
32+
str = str.slice(1);
33+
}
34+
35+
while (str[0] >= '0' && str[0] <= '9') {
36+
// console.log(str[0]);
37+
let element = Number.parseInt(str[0]);
38+
39+
if (res > bound || (res === bound && element > 7)) {
40+
if (signal) {
41+
return INT_MAX;
42+
}
43+
return INT_MIN;
44+
}
45+
46+
res = res * 10 + element;
47+
str = str.slice(1);
48+
}
49+
50+
return signal ? res : -res;
51+
};

0 commit comments

Comments
 (0)