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

Commitff3c138

Browse files
committed
add 31, 34, 36
1 parent4ef1b55 commitff3c138

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

‎Easy/36-validSudoku.js‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* use one row loop and one col loop to examine all three rules
3+
* row, column and sub-9-grid (the tricky part).
4+
*
5+
*@param {character[][]} board
6+
*@return {boolean}
7+
*/
8+
varisValidSudoku=function(board){
9+
if(!board||board.length!==9||board[0].length!==9)returnfalse;
10+
11+
for(vari=0;i<board.length;i++){
12+
varrow={};
13+
varcol={};
14+
vargrid={};
15+
for(varj=0;j<board.length;j++){
16+
if(board[i][j]!=='.'&&!(board[i][j]inrow)){
17+
row[board[i][j]]=true;
18+
}elseif(board[i][j]!=='.'&&(board[i][j]inrow)){
19+
returnfalse;
20+
}
21+
22+
if(board[j][i]!=='.'&&!(board[j][i]incol)){
23+
col[board[j][i]]=true;
24+
}elseif(board[j][i]!=='.'&&(board[j][i]incol)){
25+
returnfalse;
26+
}
27+
28+
varrowIndex=3*Math.floor(i/3)+Math.floor(j/3);
29+
varcolIndex=3*(i%3)+(j%3);
30+
if(board[rowIndex][colIndex]!=='.'&&!(board[rowIndex][colIndex]ingrid)){
31+
grid[board[rowIndex][colIndex]]=true;
32+
}elseif(board[rowIndex][colIndex]!=='.'&&(board[rowIndex][colIndex]ingrid)){
33+
returnfalse;
34+
}
35+
}
36+
}
37+
38+
returntrue;
39+
};

‎Medium/31-nextPermutation.js‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* search from right to left, find the first number smaller than it's next. (nums[i] < nums[i+1])
3+
* let's say the index is p;
4+
* have a second scan from right to left, find the first number bigger than nums[p], say the index is q,
5+
* swap element at p and q, then reverse all elements from p+1 to the last element of nums.
6+
*
7+
*@param {number[]} nums
8+
*@return {void} Do not return anything, modify nums in-place instead.
9+
*/
10+
varnextPermutation=function(nums){
11+
if(!nums||nums.length===1)return;
12+
varp=nums.length-1;
13+
varq=nums.length-1;
14+
15+
for(vari=p-1;i>=0;i--){
16+
if(nums[i+1]>nums[i]){
17+
p=i;
18+
break;
19+
}
20+
p--;
21+
}
22+
23+
for(vari=q;i>p;i--){
24+
if(nums[i]>nums[p]){
25+
vartmp=nums[p];
26+
nums[p]=nums[i];
27+
nums[i]=tmp;
28+
break;
29+
}
30+
q--;
31+
}
32+
33+
if(p===0&&q===0){
34+
reverse(nums,p);
35+
return;
36+
}
37+
38+
reverse(nums,p+1);
39+
};
40+
41+
varreverse=function(nums,i){
42+
varj=nums.length-1;
43+
while(i<j){
44+
vartmp=nums[i];
45+
nums[i]=nums[j];
46+
nums[j]=tmp;
47+
i++;
48+
j--;
49+
}
50+
};

‎Medium/34-searchforRange.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Key: Two rounds of binary search, 1) first the left index, 2) find the right index
3+
*
4+
*@param {number[]} nums
5+
*@param {number} target
6+
*@return {number[]}
7+
*/
8+
varsearchRange=function(nums,target){
9+
varlo=0;
10+
varhi=nums.length-1;
11+
varresult=[-1,-1];
12+
13+
while(lo<hi){
14+
varmid=lo+Math.floor((hi-lo)/2);
15+
if(nums[mid]<target)lo=mid+1;
16+
elsehi=mid;
17+
}
18+
if(nums[lo]!==target)returnresult;
19+
elseresult[0]=lo;
20+
21+
hi=nums.length-1;
22+
while(lo<hi){
23+
varmid=lo+Math.floor((hi-lo)/2)+1;
24+
if(nums[mid]>target)hi=mid-1;
25+
elselo=mid;
26+
}
27+
result[1]=lo;
28+
29+
returnresult;
30+
};

‎README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*[21. Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) -[Solution](./Easy/21-mergeSortedLists.js)
1414
*[24. Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) -[Solution](./Easy/24-swapNodesPairs.js)
1515
*[28. Implement strStr()](https://leetcode.com/problems/implement-strstr/) -[Solution](./Easy/28-implementstrStr.js)
16+
*[36. Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) -[Solution](./Easy/36-validSudoku.js)
1617
*[38. Count and Say](https://leetcode.com/problems/count-and-say/) -[Solution](./Easy/38-countandSay.js)
1718
*[66. Plus One](https://leetcode.com/problems/plus-one/) -[Solution](./Easy/66-plusOne.js)
1819
*[67. Add Binary](https://leetcode.com/problems/add-binary/) -[Solution](./Easy/67-addBinary.js)
@@ -75,7 +76,9 @@
7576
*[22. Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) -[Solution](./Medium/22-generateParentheses.js)
7677
*[24. Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) -[Solution](./Medium/24-swapNodesPairs.js)
7778
*[29. Divide Two Integers](https://leetcode.com/problems/divide-two-integers/) -[Solution](./Medium/29-DivideTwoIntegers.js)
78-
*[35. Search Insert Position Characters](https://oj.leetcode.com/problems/search-insert-position/) -[Solution](./Medium/35-searchInsert.js)
79+
*[31. Next Permutation](https://leetcode.com/problems/next-permutation/) -[Solution](./Medium/31-nextPermutation.js)
80+
*[31. Next Permutation](https://leetcode.com/problems/next-permutation/) -[Solution](./Medium/31-nextPermutation.js)
81+
*[34. Search for a Range](https://leetcode.com/problems/search-for-a-range/) -[Solution](./Medium/34-searchforRange.js)
7982
*[39. Combination Sum](https://oj.leetcode.com/problems/combination-sum/) -[Solution](./Medium/39-combinationSum.js)
8083
*[46. Permutations](https://leetcode.com/problems/permutations/) -[Solution](./Medium/46-permutations.js)
8184
*[48. Rotate Image](https://leetcode.com/problems/rotate-image/) -[Solution](./Medium/48-rotateImage.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp