forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.js
82 lines (78 loc) · 2.02 KB
/
Solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const isValidSudoku = function(board){
let existed = new Set();
for(let i = 0; i < 9; i++){
for(let j = 0; j < 9; j++){
let number = board[i][j];
if(board[i][j] !== '.'){
if(existed.has(number + " in row " + i )||
existed.has(number + ' in column ' + j) ||
existed.has(number + 'in block ' + parseInt(i/3) + '-' + parseInt(j/3))){
return false;
}else{
existed.add(number + " in row " + i );
existed.add(number + ' in column ' + j);
existed.add(number + 'in block ' + parseInt(i/3) + '-' + parseInt(j/3));
}
}
}
}
return true;
};
/**
* @param {character[][]} board
* @return {boolean}
*/
const isValidSudoku2 = function(board){
function lineTest(arr){
for(let i = 0; i < 9; i++){
for(let j = 0; j < 9; j++){
for(let k = j+1; k < 9; k++){
if(arr[i][j] === arr[i][k] && arr[i][j] !== '.'){
return false;
}
}
}
}
return true;
}
function columnTest(arr){
for(let i = 0; i < 9; i++){
for(let j = 0; j < 9;j++){
for(let k = j+1; k < 9; k++){
if(arr[j][i] === arr[k][i] && arr[j][i] !== '.'){
return false;
}
}
}
}
return true;
}
function squareTest(arr){
let p = [[1,1],[1,4],[1,7],
[4,1],[4,4],[4,7],
[7,1],[7,4],[7,7]];
for(let i = 0; i < p.length; i++){
let a = [];
let x = p[i][0];
let y = p[i][1];
a.push(arr[x-1][y-1]);
a.push(arr[x-1][y]);
a.push(arr[x-1][y+1]);
a.push(arr[x][y-1]);
a.push(arr[x][y]);
a.push(arr[x][y+1]);
a.push(arr[x+1][y-1]);
a.push(arr[x+1][y]);
a.push(arr[x+1][y+1]);
for(let j = 0; j < a.length; j++){
for(let k = j+1; k < a.length; k++){
if(a[j] === a[k] && a[j] !== '.'){
return false;
}
}
}
}
return true;
}
return lineTest(board)&&columnTest(board)&&squareTest(board);
};