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

Commit2e9a88c

Browse files
committed
revisit some questions
1 parente06031b commit2e9a88c

File tree

5 files changed

+100
-6
lines changed

5 files changed

+100
-6
lines changed

‎Easy/160-IntersectionofTwoLinkedLists.js‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,67 @@ var getLength = function(listHead) {
4444
}
4545
returnlength;
4646
};
47+
48+
// 2nd try without knowing the length
49+
vargetIntersectionNode=function(headA,headB){
50+
varintersection=null;
51+
varpa=headA;
52+
varpb=headB;
53+
54+
if(!pa||!pb){
55+
returnintersection;
56+
}
57+
58+
while(pa||pb){
59+
if(pa&&pb&&pa.val===pb.val){
60+
intersection=pa;
61+
}
62+
63+
// this can be replace by pa === pb
64+
while(pa&&pb&&pa.val===pb.val){
65+
pa=pa.next;
66+
pb=pb.next;
67+
}
68+
69+
if(pa===null&&pb===null){
70+
break;
71+
}elseif(pa===null){
72+
pa=headB;
73+
}elseif(pb===null){
74+
pb=headA;
75+
}else{
76+
pa=pa.next;
77+
pb=pb.next;
78+
}
79+
}
80+
81+
returnintersection;
82+
};
83+
84+
// more concise version, compared to version 2
85+
vargetIntersectionNode=function(headA,headB){
86+
varpa=headA;
87+
varpb=headB;
88+
89+
if(!pa||!pb){
90+
returnnull;
91+
}
92+
93+
while(pa&&pb&&pa!==pb){
94+
pa=pa.next;
95+
pb=pb.next;
96+
if(pa===pb){
97+
returnpa;
98+
}
99+
100+
if(!pa){
101+
pa=headB;
102+
}
103+
104+
if(!pb){
105+
pb=headA;
106+
}
107+
}
108+
109+
returnpa;
110+
};

‎Medium/152-MaxProductSubarray.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,18 @@ var maxProduct = function(nums) {
2121

2222
returnresult;
2323
};
24+
25+
// 2nd try
26+
varmaxProduct=function(nums){
27+
varmin=[nums[0]];
28+
varmax=[nums[0]];
29+
varmaxProduct=nums[0];
30+
31+
for(vari=1;i<nums.length;i++){
32+
min.push(Math.min(nums[i],min[i-1]*nums[i],max[i-1]*nums[i]));
33+
max.push(Math.max(nums[i],min[i-1]*nums[i],max[i-1]*nums[i]));
34+
maxProduct=Math.max(maxProduct,max[i]);
35+
}
36+
37+
returnmaxProduct;
38+
};

‎Medium/39-combinationSum.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
*@return {number[][]}
77
*/
88
varcombinationSum=function(candidates,target){
9+
varresult=[];
10+
varresults=[];
11+
912
candidates.sort(function(a,b){
1013
returna-b;
1114
});
12-
varresult=[];
13-
varresults=[];
1415
combinationSumHelper(candidates,target,results,result,0);
1516
returnresults;
1617
};

‎Medium/40-combinationSumII.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44
*@return {number[][]}
55
*/
66
varcombinationSum2=function(candidates,target){
7+
varresult=[];
8+
varresults=[];
9+
710
candidates.sort(function(a,b){
811
returna-b;
912
});
10-
varresult=[];
11-
varresults=[];
1213
combinationSum2Helper(candidates,result,results,target,0);
1314
returnresults;
1415
};
1516

1617
varcombinationSum2Helper=function(candidates,result,results,target,start){
1718
if(target===0){
18-
results.push(deepCopy(result.slice()));
19+
results.push(result.slice());
1920
return;
2021
}
2122

‎Medium/50-powerxn.js‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,21 @@
66
varmyPow=function(x,n){
77
if(n===0)return1;
88
if(n<0){
9-
n=-1*n;
9+
n*=-1;
1010
x=1/x;
1111
}
1212
returnn%2===0 ?myPow(x*x,n/2) :x*myPow(x*x,Math.floor(n/2));
1313
};
14+
15+
// runtime error?
16+
varmyPow=function(x,n){
17+
if(n===0){
18+
return1;
19+
}
20+
21+
if(n===1){
22+
returnx;
23+
}
24+
25+
returnn>0 ?myPow(x,n-1)*x :x/myPow(x,-n-1)*x;
26+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp