forked from xiaoyu2er/leetcode-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
036-Valid-Sudoku.js
67 lines (63 loc) · 2.35 KB
/
036-Valid-Sudoku.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
/**
* https://leetcode.com/problems/valid-sudoku/description/
* Difficulty:Medium
*
* Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
* The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
* A partially filled sudoku which is valid.
* Note:
* A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
*
*/
/**
* @param {character[][]} board
* @return {boolean}
*/
var isValidSudoku = function (board) {
for (var i = 0; i < 9; i++) {
var rowNums = [];
var colNums = [];
var cubeNums = [];
for (var j = 0; j < 9; j++) {
var ch = board[i][j];
if (ch !== '.') {
if (rowNums.indexOf(ch) > -1) return false;
rowNums.push(ch);
}
ch = board[j][i];
if (ch !== '.') {
if (colNums.indexOf(ch) > -1) return false;
colNums.push(ch);
}
var row = Math.floor(i / 3) * 3 + Math.floor(j / 3);
var col = i % 3 * 3 + j % 3;
// console.log(i, j, row, col);
ch = board[row][col];
if (ch !== '.') {
if (cubeNums.indexOf(ch) > -1) return false;
cubeNums.push(ch);
}
}
}
return true;
};
// console.log(isValidSudoku([
// [".", "8", "7", "6", "5", "4", "3", "2", "1"],
// ["2", ".", ".", ".", ".", ".", ".", ".", "."],
// ["3", ".", ".", ".", ".", ".", ".", ".", "."],
// ["4", ".", ".", ".", ".", ".", ".", ".", "."],
// ["5", ".", ".", ".", ".", ".", ".", ".", "."],
// ["6", ".", ".", ".", ".", ".", ".", ".", "."],
// ["7", ".", ".", ".", ".", ".", ".", ".", "."],
// ["8", ".", ".", ".", ".", ".", ".", ".", "."],
// ["9", ".", ".", ".", ".", ".", ".", ".", "."]]));
console.log(isValidSudoku([
[".", ".", "4", ".", ".", ".", "6", "3", "."],
[".", ".", ".", ".", ".", ".", ".", ".", "."],
["5", ".", ".", ".", ".", ".", ".", "9", "."],
[".", ".", ".", "5", "6", ".", ".", ".", "."],
["4", ".", "3", ".", ".", ".", ".", ".", "1"],
[".", ".", ".", "7", ".", ".", ".", ".", "."],
[".", ".", ".", "5", ".", ".", ".", ".", "."],
[".", ".", ".", ".", ".", ".", ".", ".", "."],
[".", ".", ".", ".", ".", ".", ".", ".", "."]]))