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

Commit3fd0aeb

Browse files
author
zongyanqi
committed
add 037 039 040 046 047 500 57 617 728
1 parent6689584 commit3fd0aeb

9 files changed

+445
-0
lines changed

‎037-Sudoku-Solver.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* https://leetcode.com/problems/sudoku-solver/description/
3+
* Difficulty:Hard
4+
*
5+
* Write a program to solve a Sudoku puzzle by filling the empty cells.
6+
* Empty cells are indicated by the character '.'.
7+
* You may assume that there will be only one unique solution.
8+
*/
9+
10+
/**
11+
*@param {character[][]} board
12+
*@return {void} Do not return anything, modify board in-place instead.
13+
*/
14+
varsolveSudoku=function(board){
15+
solve(board);
16+
console.log(board);
17+
};
18+
19+
functionsolve(board){
20+
for(vari=0;i<9;i++){
21+
for(varj=0;j<9;j++){
22+
varch=board[i][j];
23+
if(ch==='.'){
24+
for(vark=1;k<=9;k++){
25+
26+
if(isValid(i,j,board,''+k)){
27+
28+
board[i][j]=''+k;
29+
// console.log(board);
30+
// console.log('-------------');
31+
if(solve(board)){
32+
// console.log(board);
33+
// console.log('-------------');
34+
returntrue;
35+
}else{
36+
board[i][j]='.';
37+
}
38+
}
39+
}
40+
returnfalse;
41+
}
42+
}
43+
}
44+
returntrue;
45+
}
46+
47+
functionisValid(row,col,board,t){
48+
49+
for(vari=0;i<9;i++){
50+
varch=board[row][i];
51+
if(ch===t)returnfalse;
52+
53+
ch=board[i][col];
54+
if(ch===t)returnfalse;
55+
56+
ch=board[Math.floor(row/3)*3+Math.floor(i/3)][Math.floor(col/3)*3+i%3];
57+
// if (row === 0 && col === 8) {
58+
// console.log('~ ', Math.floor(row / 3) * 3 + Math.floor(i / 3), Math.floor(row / 3) * 3 + i % 3, ch);
59+
// }
60+
if(ch===t)returnfalse;
61+
}
62+
returntrue;
63+
64+
}
65+
66+
console.log(solveSudoku([
67+
[".",".","9","7","4","8",".",".","."],
68+
["7",".",".",".",".",".",".",".","."],
69+
[".","2",".","1",".","9",".",".","."],
70+
[".",".","7",".",".",".","2","4","."],
71+
[".","6","4",".","1",".","5","9","."],
72+
[".","9","8",".",".",".","3",".","."],
73+
[".",".",".","8",".","3",".","2","."],
74+
[".",".",".",".",".",".",".",".","6"],
75+
[".",".",".","2","7","5","9",".","."]
76+
]));
77+
78+
console.log([
79+
["5","1","9","7","4","8","6","3","2"],
80+
["7","8","3","6","5","2","4","1","9"],
81+
["4","2","6","1","3","9","8","7","5"],
82+
["3","5","7","9","8","6","2","4","1"],
83+
["2","6","4","3","1","7","5","9","8"],
84+
["1","9","8","5","2","4","3","6","7"],
85+
["9","7","5","8","6","3","1","2","4"],
86+
["8","3","2","4","9","1","7","5","6"],
87+
["6","4","1","2","7","5","9","8","3"]
88+
])

‎039-Combination-Sum.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* https://leetcode.com/problems/combination-sum/description/
3+
* Difficulty:Medium
4+
*
5+
* Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
6+
* The same repeated number may be chosen from C unlimited number of times.
7+
* Note:
8+
* All numbers (including target) will be positive integers.
9+
* The solution set must not contain duplicate combinations.
10+
* For example, given candidate set [2, 3, 6, 7] and target 7,
11+
* A solution set is:
12+
* [
13+
* [7],
14+
* [2, 2, 3]
15+
* ]
16+
*/
17+
18+
/**
19+
*@param {number[]} candidates
20+
*@param {number} target
21+
*@return {number[][]}
22+
*/
23+
varcombinationSum=function(candidates,target){
24+
25+
varres=[];
26+
vartemp=[];
27+
helper(res,temp,candidates,target,0);
28+
returnres;
29+
30+
};
31+
32+
functionhelper(res,temp,candidates,target,start){
33+
34+
if(target===0){
35+
res.push([...temp]);
36+
return;
37+
}
38+
39+
for(vari=start;i<candidates.length;i++){
40+
if(candidates[i]<=target){
41+
temp.push(candidates[i]);
42+
helper(res,temp,candidates,target-candidates[i],i);
43+
temp.length-=1;
44+
}
45+
46+
}
47+
}
48+
49+
console.log(combinationSum([1,2,3,5,6,7],7));
50+
console.log(combinationSum([7,2,3,5,6,1],7));

‎040-Combination-Sum-II.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
3+
* Each number in C may only be used once in the combination.
4+
* Note:
5+
* All numbers (including target) will be positive integers.
6+
* The solution set must not contain duplicate combinations.
7+
* For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
8+
* A solution set is:
9+
* [
10+
* [1, 7],
11+
* [1, 2, 5],
12+
* [2, 6],
13+
* [1, 1, 6]
14+
* ]
15+
*/
16+
17+
/**
18+
*@param {number[]} candidates
19+
*@param {number} target
20+
*@return {number[][]}
21+
*/
22+
varcombinationSum2=function(candidates,target){
23+
24+
varres=[];
25+
vartemp=[];
26+
candidates.sort((b,a)=>b-a);
27+
helper(res,temp,candidates,target,0);
28+
returnres;
29+
};
30+
31+
functionhelper(res,temp,candidates,target,start){
32+
if(target===0){
33+
returnres.push([...temp]);
34+
}
35+
36+
for(vari=start;i<candidates.length&&candidates[i]<=target;i++){
37+
if(i===start||candidates[i]!==candidates[i-1]){
38+
temp.push(candidates[i]);
39+
helper(res,temp,candidates,target-candidates[i],i+1);
40+
temp.length-=1;
41+
}
42+
}
43+
}
44+
45+
console.log(combinationSum2([10,1,2,7,6,1,5],8));

‎046-Permutations.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* https://leetcode.com/problems/permutations/description/
3+
* Difficulty:Medium
4+
*
5+
* Given a collection of distinct numbers, return all possible permutations.
6+
* For example,
7+
* [1,2,3] have the following permutations:
8+
* [
9+
* [1,2,3],
10+
* [1,3,2],
11+
* [2,1,3],
12+
* [2,3,1],
13+
* [3,1,2],
14+
* [3,2,1]
15+
* ]
16+
*/
17+
18+
/**
19+
*@param {number[]} nums
20+
*@return {number[][]}
21+
*/
22+
varpermute=function(nums){
23+
if(!nums.length)return[];
24+
varres=[[]];
25+
for(vari=0;i<nums.length;i++){
26+
varlen=res.length;
27+
for(varj=0;j<len;j++){
28+
varoldArr=res.shift();
29+
for(vark=0;k<=oldArr.length;k++){
30+
varnewArr=oldArr.slice();
31+
newArr.splice(k,0,nums[i]);
32+
res.push(newArr);
33+
}
34+
}
35+
}
36+
returnres;
37+
};
38+
console.log(permute([1,2,3]));

‎047-Permutations-II.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* https://leetcode.com/problems/permutations-ii/description/
3+
* Difficulty:Medium
4+
*
5+
* Given a collection of numbers that might contain duplicates, return all possible unique permutations.
6+
* For example,
7+
* [1,1,2] have the following unique permutations:
8+
* [
9+
* [1,1,2],
10+
* [1,2,1],
11+
* [2,1,1]
12+
* ]
13+
*/
14+
15+
/**
16+
*@param {number[]} nums
17+
*@return {number[][]}
18+
*/
19+
varpermuteUnique=function(nums){
20+
if(!nums.length)return[];
21+
nums.sort((a,b)=>a-b);
22+
varres=[[]];
23+
for(vari=0;i<nums.length;i++){
24+
varlen=res.length;
25+
for(varj=0;j<len;j++){
26+
varoldArr=res.shift();
27+
if(i>0&&nums[i]===nums[i-1]){
28+
vark=oldArr.lastIndexOf(nums[i]);
29+
}else{
30+
k=0;
31+
}
32+
for(;k<=oldArr.length;k++){
33+
34+
if(k===oldArr.length||nums[i]!==oldArr[k]){
35+
varnewArr=oldArr.slice();
36+
newArr.splice(k,0,nums[i]);
37+
// console.log(oldArr, newArr);
38+
res.push(newArr);
39+
}
40+
41+
}
42+
}
43+
}
44+
returnres;
45+
};
46+
47+
console.log(permuteUnique([1,2,2,1]));

‎500-Keyboard-Row.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* https://leetcode.com/problems/keyboard-row/description/
3+
* Difficulty:Easy
4+
*
5+
* Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.
6+
*
7+
* Example 1:
8+
* Input: ["Hello", "f", "Dad", "Peace"]
9+
* Output: ["Alaska", "Dad"]
10+
* Note:
11+
* You may use one character in the keyboard more than once.
12+
* You may assume the input string will only contain letters of alphabet.
13+
*/
14+
15+
/**
16+
*@param {string[]} words
17+
*@return {string[]}
18+
*/
19+
varfindWords=function(words){
20+
21+
vars='qwertyuiopasdfghjklzxcvbnm';
22+
returnwords.filter(w=>{
23+
if(!w)returntrue;
24+
w=w.toLowerCase();
25+
vart=row(w[0]);
26+
for(vari=1;i<w.length;i++){
27+
if(t!==row(w[i]))returnfalse;
28+
}
29+
returntrue;
30+
});
31+
32+
functionrow(ch){
33+
vari=s.indexOf(ch);
34+
if(i<10)return0;
35+
if(i<19)return1;
36+
return2;
37+
}
38+
39+
};
40+
console.log(findWords(["Hello","Alaska","Dad","Peace"]))

‎557-Reverse-Words-in-a-String-III.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* https://leetcode.com/problems/reverse-words-in-a-string-iii/description/
3+
* Difficulty:Easy
4+
*
5+
* Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
6+
* Example 1:
7+
* Input: "Let's take LeetCode contest"
8+
* Output: "s'teL ekat edoCteeL tsetnoc"
9+
* Note: In the string, each word is separated by single space and there will not be any extra space in the string.
10+
*/
11+
12+
/**
13+
*@param {string} s
14+
*@return {string}
15+
*/
16+
varreverseWords=function(s){
17+
returns.split(' ').map(w=>w.split('').reverse().join('')).join(' ');
18+
};
19+
20+
console.log(reverseWords(`Let's take LeetCode contest`))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp