-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36.valid-sudoku.go
More file actions
46 lines (43 loc) · 922 Bytes
/
36.valid-sudoku.go
File metadata and controls
46 lines (43 loc) · 922 Bytes
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
/*
* @lc app=leetcode id=36 lang=golang
*
* [36] Valid Sudoku
*/
// @lc code=start
func isValidSudoku(board [][]byte) bool {
// 0ならおける、1ならおけない
boardFlag := make([][][]int, 9)
for i := range boardFlag {
boardFlag[i] = make([][]int, 9)
for j := range boardFlag[i] {
boardFlag[i][j] = make([]int, 9)
}
}
for i := 0; i < 9; i++ {
for j := 0; j < 9; j++ {
if board[i][j] == '.' {
continue
}
num := board[i][j] - '1'
if boardFlag[i][j][num] == 1 {
return false
}
boardFlag = updateBoarFlag(i, j, int(num), boardFlag)
}
}
return true
}
func updateBoarFlag(i, j, num int, boardFlag [][][]int) [][][]int {
for k := 0; k < 9; k++ {
boardFlag[i][j][k] = 1
boardFlag[k][j][num] = 1
boardFlag[i][k][num] = 1
}
for k := 0; k < 3; k++ {
for l := 0; l < 3; l++ {
boardFlag[(i/3)*3+k][(j/3)*3+l][num] = 1
}
}
return boardFlag
}
// @lc code=end