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

Commit403abae

Browse files
committed
add 23, 25, 32 modified 21, 24
1 parent8b8f1d9 commit403abae

File tree

6 files changed

+124
-46
lines changed

6 files changed

+124
-46
lines changed

‎Easy/21-mergeSortedLists.js‎

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,7 @@
1313
varmergeTwoLists=function(l1,l2){
1414
varl3=newListNode();
1515
varl3Head=l3;
16-
if(l1===null&&l2===null){
17-
returnnull;
18-
}
19-
while(true){
20-
if(l1===null&&l2!==null){
21-
l3.val=l2.val;
22-
l2=l2.next;
23-
}elseif(l2===null&&l1!==null){
24-
l3.val=l1.val;
25-
l1=l1.next;
26-
}elseif(l1.val<=l2.val){
27-
l3.val=l1.val;
28-
l1=l1.next;
29-
}else{
30-
l3.val=l2.val;
31-
l2=l2.next;
32-
}
33-
if(l1===null&&l2===null){
34-
break;
35-
}
36-
l3.next=newListNode();
37-
l3=l3.next;
38-
}
39-
40-
returnl3Head;
41-
};
42-
43-
// discard first dummy node in the new list which contains the merged result.
44-
// this is good and straightforward
45-
varmergeTwoLists=function(l1,l2){
46-
varl3=newListNode();
47-
varl3Head=l3;
48-
while(true){
49-
if(l1===null&&l2===null){
50-
break;
51-
}
52-
if(l1===null&&l2!==null){
53-
l3.next=l2;
54-
break;
55-
}
56-
if(l2===null&&l1!==null){
57-
l3.next=l1;
58-
break;
59-
}
16+
while(l1&&l2){
6017
if(l1.val<=l2.val){
6118
l3.next=l1;
6219
l1=l1.next;
@@ -66,6 +23,8 @@ var mergeTwoLists = function(l1, l2) {
6623
}
6724
l3=l3.next;
6825
}
26+
if(!l1)l3.next=l2;
27+
if(!l2)l3.next=l1
6928

7029
returnl3Head.next;
7130
};

‎Medium/24-swapNodesPairs.js‎renamed to ‎Easy/24-swapNodesPairs.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,35 @@ var swapPairs = function(head) {
3030

3131
returnnewHead;
3232
};
33+
34+
// re write
35+
varswapPairs=function(head){
36+
if(!head||!head.next)returnhead;
37+
varp=head;
38+
varpPrev=head;
39+
varnewHead=head.next;
40+
while(p){
41+
varpNext=p.next;
42+
if(pNext){
43+
pPrev.next=pNext;
44+
p.next=pNext.next;
45+
pNext.next=p;
46+
pPrev=p;
47+
p=p.next;
48+
}else{
49+
break;
50+
}
51+
}
52+
53+
returnnewHead;
54+
};
55+
56+
// recursion version, clean Code
57+
varswapPairs=function(head){
58+
if(!head||!head.next)returnhead;
59+
varp=head;
60+
varpNext=p.next;
61+
p.next=swapPairs(pNext.next);
62+
pNext.next=p;
63+
returnpNext;
64+
};

‎Hard/23-MergeKSortedLists.js‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Key: using priority queue is a commonly used solution and it has N*O(logK)
3+
* However, in JavaScript, there is no existing priority queue implementation,
4+
* so the new idea is to merge two sorted list every time. divide the lists into two
5+
* halves (divide-and-conque), and merge the two halves. Recursively merge.
6+
*
7+
* Definition for singly-linked list.
8+
* function ListNode(val) {
9+
* this.val = val;
10+
* this.next = null;
11+
* }
12+
*/
13+
/**
14+
*@param {ListNode[]} lists
15+
*@return {ListNode}
16+
*/
17+
varmergeKLists=function(lists){
18+
if(lists.length===0)returnlists;
19+
if(lists.length===1)returnlists[0];
20+
if(lists.length===2)returnmergeTwoLists(lists[0],lists[1]);
21+
varlistNums=lists.length;
22+
varfirstHalf=lists.slice(0,Math.floor(listNums/2));
23+
varsecondHalf=lists.slice(Math.floor(listNums/2),listNums);
24+
returnmergeTwoLists(mergeKLists(firstHalf),mergeKLists(secondHalf));
25+
};
26+
27+
varmergeTwoLists=function(l1,l2){
28+
varl3=newListNode();
29+
varl3Head=l3;
30+
while(l1&&l2){
31+
if(l1.val<=l2.val){
32+
l3.next=l1;
33+
l1=l1.next;
34+
}else{
35+
l3.next=l2;
36+
l2=l2.next;
37+
}
38+
l3=l3.next;
39+
}
40+
if(!l1)l3.next=l2;
41+
if(!l2)l3.next=l1
42+
43+
returnl3Head.next;
44+
};

‎Hard/25-reverseNodesInKGroup.js‎

Whitespace-only changes.

‎Hard/32-longestValidParentheses.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
*@param {string} s
3+
*@return {number}
4+
*/
5+
varlongestValidParentheses=function(s){
6+
varstack=[];
7+
varresult=0;
8+
varmaxLength=0;
9+
10+
for(vari=0;i<s.length;i++){
11+
if(s[i]==='('){
12+
stack.push(i);
13+
}
14+
if(s[i]===')'){
15+
if(stack.length>0){
16+
if(s[stack[stack.length-1]]==='('){
17+
stack.pop();
18+
}else{
19+
stack.push(i);
20+
}
21+
}elseif(stack.length===0){
22+
stack.push(i);
23+
}
24+
}
25+
}
26+
27+
if(stack.length===0)returns.length;
28+
29+
varright=s.length;
30+
while(stack.length>0){
31+
varleft=stack[stack.length-1];
32+
maxLength=right-left-1;
33+
result=Math.max(maxLength,result);
34+
right=stack.pop();
35+
}
36+
if(stack.length===0){
37+
result=Math.max(result,right);
38+
}
39+
40+
returnresult;
41+
};

‎README.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
*[16. 3Sum Closest](https://leetcode.com/problems/3sum-closest/) -[Solution](./Medium/16-3sumClosest.js)
7676
*[17. Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) -[Solution](./Medium/17-LetterCombinationsPhoneNumber.js)
7777
*[22. Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) -[Solution](./Medium/22-generateParentheses.js)
78-
*[24. Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) -[Solution](./Medium/24-swapNodesPairs.js)
7978
*[29. Divide Two Integers](https://leetcode.com/problems/divide-two-integers/) -[Solution](./Medium/29-DivideTwoIntegers.js)
8079
*[31. Next Permutation](https://leetcode.com/problems/next-permutation/) -[Solution](./Medium/31-nextPermutation.js)
8180
*[31. Next Permutation](https://leetcode.com/problems/next-permutation/) -[Solution](./Medium/31-nextPermutation.js)
@@ -122,4 +121,7 @@
122121
*[337. House Robber III](https://leetcode.com/problems/house-robber-iii/) -[Solution](./Medium/337-houseRobberIII.js)
123122

124123
#####Hard
125-
*[33. Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) -[Solution](./Medium/33-searchRotatedSortedArray.js)
124+
*[23. Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/) -[Solution](./Hard/23-MergeKSortedLists.js)
125+
*[25. Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/) -[Solution](./Hard/25-reverseNodesInKGroup.js)
126+
*[32. Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/) -[Solution](./Hard/32-longestValidParentheses.js)
127+
*[33. Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) -[Solution](./Hard/33-searchRotatedSortedArray.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp