Skip to content

Commit 3fd0aeb

Browse files
author
zongyanqi
committed
add 037 039 040 046 047 500 57 617 728
1 parent 6689584 commit 3fd0aeb

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+
var solveSudoku = function (board) {
15+
solve(board);
16+
console.log(board);
17+
};
18+
19+
function solve(board) {
20+
for (var i = 0; i < 9; i++) {
21+
for (var j = 0; j < 9; j++) {
22+
var ch = board[i][j];
23+
if (ch === '.') {
24+
for (var k = 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+
return true;
35+
} else {
36+
board[i][j] = '.';
37+
}
38+
}
39+
}
40+
return false;
41+
}
42+
}
43+
}
44+
return true;
45+
}
46+
47+
function isValid(row, col, board, t) {
48+
49+
for (var i = 0; i < 9; i++) {
50+
var ch = board[row][i];
51+
if (ch === t) return false;
52+
53+
ch = board[i][col];
54+
if (ch === t) return false;
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) return false;
61+
}
62+
return true;
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+
var combinationSum = function (candidates, target) {
24+
25+
var res = [];
26+
var temp = [];
27+
helper(res, temp, candidates, target, 0);
28+
return res;
29+
30+
};
31+
32+
function helper(res, temp, candidates, target, start) {
33+
34+
if (target === 0) {
35+
res.push([...temp]);
36+
return;
37+
}
38+
39+
for (var i = 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+
var combinationSum2 = function (candidates, target) {
23+
24+
var res = [];
25+
var temp = [];
26+
candidates.sort((b, a) => b - a);
27+
helper(res, temp, candidates, target, 0);
28+
return res;
29+
};
30+
31+
function helper(res, temp, candidates, target, start) {
32+
if (target === 0) {
33+
return res.push([...temp]);
34+
}
35+
36+
for (var i = 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+
var permute = function (nums) {
23+
if (!nums.length) return [];
24+
var res = [[]];
25+
for (var i = 0; i < nums.length; i++) {
26+
var len = res.length;
27+
for (var j = 0; j < len; j++) {
28+
var oldArr = res.shift();
29+
for (var k = 0; k <= oldArr.length; k++) {
30+
var newArr = oldArr.slice();
31+
newArr.splice(k, 0, nums[i]);
32+
res.push(newArr);
33+
}
34+
}
35+
}
36+
return res;
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+
var permuteUnique = function (nums) {
20+
if (!nums.length) return [];
21+
nums.sort((a, b) => a - b);
22+
var res = [[]];
23+
for (var i = 0; i < nums.length; i++) {
24+
var len = res.length;
25+
for (var j = 0; j < len; j++) {
26+
var oldArr = res.shift();
27+
if (i > 0 && nums[i] === nums[i - 1]) {
28+
var k = 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+
var newArr = oldArr.slice();
36+
newArr.splice(k, 0, nums[i]);
37+
// console.log(oldArr, newArr);
38+
res.push(newArr);
39+
}
40+
41+
}
42+
}
43+
}
44+
return res;
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+
var findWords = function (words) {
20+
21+
var s = 'qwertyuiopasdfghjklzxcvbnm';
22+
return words.filter(w => {
23+
if (!w) return true;
24+
w = w.toLowerCase();
25+
var t = row(w[0]);
26+
for (var i = 1; i < w.length; i++) {
27+
if (t !== row(w[i])) return false;
28+
}
29+
return true;
30+
});
31+
32+
function row(ch) {
33+
var i = s.indexOf(ch);
34+
if (i < 10) return 0;
35+
if (i < 19) return 1;
36+
return 2;
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+
var reverseWords = function (s) {
17+
return s.split(' ').map(w => w.split('').reverse().join('')).join(' ');
18+
};
19+
20+
console.log(reverseWords(`Let's take LeetCode contest`))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy