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

Commit8b8f1d9

Browse files
committed
add 20, 34, 40
1 parentff3c138 commit8b8f1d9

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

‎Easy/20-validParentheses.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
*@param {string} s
3+
*@return {boolean}
4+
*/
5+
varisValid=function(s){
6+
varstack=[];
7+
for(vari=0;i<s.length;i++){
8+
if(s[i]==='('||s[i]==='['||s[i]==='{'){
9+
stack.push(s[i]);
10+
}
11+
12+
if(s[i]===')'){
13+
if(stack[stack.length-1]!=='(')returnfalse;
14+
elsestack.pop()
15+
}
16+
17+
if(s[i]===']'){
18+
if(stack[stack.length-1]!=='[')returnfalse;
19+
elsestack.pop();
20+
}
21+
22+
if(s[i]==='}'){
23+
if(stack[stack.length-1]!=='{')returnfalse;
24+
elsestack.pop();
25+
}
26+
}
27+
28+
if(stack.length===0)returntrue;
29+
elsereturnfalse;
30+
};

‎Medium/34-searchforRange.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var searchRange = function(nums, target) {
2020

2121
hi=nums.length-1;
2222
while(lo<hi){
23+
// this +1 is very important!!! Make mid biased to the right
2324
varmid=lo+Math.floor((hi-lo)/2)+1;
2425
if(nums[mid]>target)hi=mid-1;
2526
elselo=mid;

‎Medium/40-combinationSumII.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
*@param {number[]} candidates
3+
*@param {number} target
4+
*@return {number[][]}
5+
*/
6+
varcombinationSum2=function(candidates,target){
7+
candidates.sort(function(a,b){
8+
returna-b;
9+
});
10+
varresult=[];
11+
varresults=[];
12+
combinationSum2Helper(candidates,result,results,target,0);
13+
returnresults;
14+
};
15+
16+
varcombinationSum2Helper=function(candidates,result,results,target,start){
17+
if(target===0){
18+
results.push(deepCopy(result));
19+
return;
20+
}
21+
22+
for(vari=start;i<candidates.length;i++){
23+
if(target<0)break;
24+
if(i>start&&candidates[i]===candidates[i-1]){
25+
continue;
26+
}else{
27+
result.push(candidates[i]);
28+
combinationSum2Helper(candidates,result,results,target-candidates[i],i+1);
29+
result.pop();
30+
}
31+
}
32+
};
33+
34+
vardeepCopy=function(nums){
35+
varnewNums=[];
36+
for(vari=0;i<nums.length;i++)newNums.push(nums[i]);
37+
returnnewNums;
38+
};

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*[9. Palindrome Number](https://leetcode.com/problems/palindrome-number/) -[Solution](./Easy/9-palindromeNumber.js)
1111
*[14. Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/) -[Solution](./Easy/14-longestCommonPrefix.js)
1212
*[19. Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) -[Solution](./Easy/19-removeNthNodeFromEndofList.js)
13+
*[20. Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) -[Solution](./Easy/20-validParentheses.js)
1314
*[21. Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) -[Solution](./Easy/21-mergeSortedLists.js)
1415
*[24. Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) -[Solution](./Easy/24-swapNodesPairs.js)
1516
*[28. Implement strStr()](https://leetcode.com/problems/implement-strstr/) -[Solution](./Easy/28-implementstrStr.js)
@@ -80,6 +81,7 @@
8081
*[31. Next Permutation](https://leetcode.com/problems/next-permutation/) -[Solution](./Medium/31-nextPermutation.js)
8182
*[34. Search for a Range](https://leetcode.com/problems/search-for-a-range/) -[Solution](./Medium/34-searchforRange.js)
8283
*[39. Combination Sum](https://oj.leetcode.com/problems/combination-sum/) -[Solution](./Medium/39-combinationSum.js)
84+
*[40. Combination Sum II](https://oj.leetcode.com/problems/combination-sum-ii/) -[Solution](./Medium/40-combinationSumII.js)
8385
*[46. Permutations](https://leetcode.com/problems/permutations/) -[Solution](./Medium/46-permutations.js)
8486
*[48. Rotate Image](https://leetcode.com/problems/rotate-image/) -[Solution](./Medium/48-rotateImage.js)
8587
*[53. Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) -[Solution](./Medium/53-maximumSubarray.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp