Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8bbef62

Browse files
committed
add: 3Sum
1 parent8811db6 commit8bbef62

File tree

5 files changed

+156
-100
lines changed

5 files changed

+156
-100
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* res.js
3+
*@authors Joe Jiang (hijiangtao@gmail.com)
4+
*@date 2017-04-03 23:45:47
5+
*
6+
*@param {number[]} nums
7+
*@return {number[][]}
8+
*/
9+
letthreeSum=function(nums){
10+
nums.sort(function(a,b){
11+
returna-b;
12+
});
13+
14+
letnumlen=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(leti=0;i<eInd;i++){
24+
letrevi=-nums[i],
25+
j=i+1,
26+
k=numlen-1;
27+
28+
if(revi<0){
29+
break;
30+
}
31+
32+
while(j<k){
33+
letnorj=nums[j],
34+
nork=nums[k];
35+
if(norj+nork>revi){
36+
k--;
37+
}elseif(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+
returnres;
55+
};

‎src/longest-common-prefix/res.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,35 @@
1010
*@return {string}
1111
*/
1212
letlongestCommonPrefix=function(strs){
13-
if(strs===''||strs.length===0){
14-
return'';
15-
}
16-
17-
letprefix='',
18-
strslen=strs.length,
19-
strlen=strs[0].length;
20-
21-
for(leti=0;i<strlen;i++){
22-
letjud=true;
23-
24-
for(letj=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-
returnprefix;
35-
}
36-
}
37-
38-
returnprefix;
13+
if(strs===''||strs.length===0){
14+
return'';
15+
}
16+
17+
letprefix='',
18+
strslen=strs.length,
19+
strlen=strs[0].length;
20+
21+
for(leti=0;i<strlen;i++){
22+
letjud=true;
23+
24+
for(letj=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+
returnprefix;
35+
}
36+
}
37+
38+
returnprefix;
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

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,39 @@
99
*@return {number[][]}
1010
*/
1111
letpermute=function(nums){
12-
letnumlen=nums.length,
13-
res=[];
14-
15-
if(numlen===0){
16-
returnres;
17-
}elseif(numlen===1){
18-
res.push(nums);
19-
returnres;
20-
}
21-
22-
returnsubpermute([],nums);
12+
letnumlen=nums.length,
13+
res=[];
14+
15+
if(numlen===0){
16+
returnres;
17+
}elseif(numlen===1){
18+
res.push(nums);
19+
returnres;
20+
}
21+
22+
returnsubpermute([],nums);
2323
};
2424

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

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

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,43 @@
99
*@return {number}
1010
*/
1111
letmyAtoi=function(str){
12-
constINT_MAX=2147483647,
13-
INT_MIN=-2147483648,
14-
bound=Number.parseInt(INT_MAX/10);
15-
letstrlen=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-
return0;
26-
}
27-
28-
if(str[0]==='-'){
29-
signal=0;
30-
str=str.slice(1);
31-
}elseif(str[0]==="+"){
32-
str=str.slice(1);
33-
}
34-
35-
while(str[0]>='0'&&str[0]<='9'){
36-
// console.log(str[0]);
37-
letelement=Number.parseInt(str[0]);
38-
39-
if(res>bound||(res===bound&&element>7)){
40-
if(signal){
41-
returnINT_MAX;
42-
}
43-
returnINT_MIN;
44-
}
45-
46-
res=res*10+element;
47-
str=str.slice(1);
48-
}
49-
50-
returnsignal?res:-res;
51-
};
12+
constINT_MAX=2147483647,
13+
INT_MIN=-2147483648,
14+
bound=Number.parseInt(INT_MAX/10);
15+
letstrlen=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+
return0;
26+
}
27+
28+
if(str[0]==='-'){
29+
signal=0;
30+
str=str.slice(1);
31+
}elseif(str[0]==="+"){
32+
str=str.slice(1);
33+
}
34+
35+
while(str[0]>='0'&&str[0]<='9'){
36+
// console.log(str[0]);
37+
letelement=Number.parseInt(str[0]);
38+
39+
if(res>bound||(res===bound&&element>7)){
40+
if(signal){
41+
returnINT_MAX;
42+
}
43+
returnINT_MIN;
44+
}
45+
46+
res=res*10+element;
47+
str=str.slice(1);
48+
}
49+
50+
returnsignal?res :-res;
51+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp