2022-1-21leetcode
36.有效的husddddd
var isValidSudoku = function(board){
const rows = new Array(9).fill(0).map(()=> new Array(9).fill(0));
const columns = new Array(9).fill(0).map(()=> new Array(9).fill(0));
const subboxes = new Array(3).fill(0).map(()=> new Array(3).fill(0).map(()=> new Array(9).fill(0)));
for(let i = 0 ; i< 9; i++){
for(let j = 0;j<9;j++){
if(c !’.’){
const index = c.charCodeAt() - ‘0’.charCodeAt() -1;
rows[i][index]++;
columns[j][index]++;
subboxes[Math.floor(i/3)][Math.floor(j/3)][index]++;
if(rows[i][index] > 1||columns[j][index] >1 || subboxes[Math.floor(i/3)][j/3][index] > 1){
return false
}
}
}
}
return true;
}
41. 缺diyigezhengshu
function test(num){
for(let i = 0;i<num.length;i++){
while(
num[i] >= 1 && num[i] <= num.length
&& num[num[i]-1] ! num[i]
){ const temp = num[num[i] - 1]
num[num-1] = num[i]
num[i] = temp}
}
for(let i =0;i<num.length;i++){
if(num[i] != i+1){
return i+1 } }}
console.log(test([1,2,3,5]));
jiaohuan后 ,就是i+1
quanpailei
46.全排列
var permute = function(nums){
const res = [], path = [];
backtracking(nums,nums.length, []);
return res;
function backtracking(n,k,used){
if( path.length === k){
res.push(Array.from(path));
return;
}
for(let i = 0; i < k ; i++){
if(used[i]) continue;
path.push(n[i]);
used[i] = true;
backtracking(n,k,used);
path.pop();
used[i] = false;
}
}
}
47.全排列2
var permute = function(nums){
nums.sort((a,b) => {
return a-b;
})
let result =[];
let path = [];
function backtracking(used){
if( path.length === nums.length){
result.push(path.slice());
return;
}
for(let i = 0; i < nums.length ; i++){
if(i>0 && nums[i] === nums[i - 1] && !used[i - 1]) continue;
if(!used[i]){
path.push(nums[i]);
used[i] = true;
backtracking(n,k,used);
path.pop();
used[i] = false
}
}
}
backtracking([])
return result
}
回朔 增加重复deltiaojianengji判读相比
function test(x,n){
if(n< 0 ){ n = Math.abs(n); x = 1/x;} if(n === 1)return x;if(n === 0)return 1;let divisor = 1
while(n>1){
if(n & 1){ divisor *= x; n++; } x **=2; n>>>=1;
}
return x / divisor
}
console.log(test(2,10));
幂次方
51 .nhuanghou
const solveNQueens = (n) =>{
const board = new Array(n);
for(let i = 0;i<n;i++){
board[i] = new Array(n).fill(’.’);
}
const cols = new Set();
const diag1 = new Set();
const diag2 = new Set();
const res = [];
const helper = (row) => {
if(row == n){
const strinsBoard = board.slice();
for(let i= 0; i<n;i++){
stringsBoard[i] = stringBoard[i].join('');
}
res.push(strinsBoard);
return;
}
for(let col = 0;col < n;col++){
if(!cols.has(col) && !diag1.has(row + col) && diag2.has(row - col)){
board[row][col] = 'Q';
cols.add(col);
diag2.add(row - col);
diag1.add(row + col);
helper(row + 1); //zuzhi
board[row][col] = '.'; //撤销该点的皇后
cols.delete(col);
diag2.delete(row - col);
diag1.delete(row + col);
}
}
};
helper(0);
return res;
}
jingdianN
52.n ||
const backtrack = (n, row, columns, diagonals1, diagonals2) => { if (row === n) { return 1; } else { let count = 0; for (let i = 0; i < n; i++) { if (columns.has(i)) { continue; } const diagonal1 = row - i; if (diagonals1.has(diagonal1)) { continue; } const diagonal2 = row + i; if (diagonals2.has(diagonal2)) { continue; } columns.add(i); diagonals1.add(diagonal1); diagonals2.add(diagonal2); count += backtrack(n, row + 1, columns, diagonals1, diagonals2); columns.delete(i); diagonals1.delete(diagonal1); diagonals2.delete(diagonal2); } return count; } } var totalNQueens = function(n) { const columns = new Set(); const diagonals1 = new Set(); const diagonals2 = new Set(); return backtrack(n, 0, columns, diagonals1, diagonals2); };
回溯问题