-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathleetcode0036.go
51 lines (44 loc) · 932 Bytes
/
leetcode0036.go
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
/*
LeetCode 36: https://leetcode.com/problems/valid-sudoku/
*/
package leetcode
func isValidSudoku(board [][]byte) bool {
insertIfNotExist := func(set map[byte]bool, b byte) bool {
if _, exist := set[b]; exist {
return false
}
set[b] = true
return true
}
for i := 0; i < 9; i++ {
set := map[byte]bool{}
for j := 0; j < 9; j++ {
if board[i][j] != '.' && !insertIfNotExist(set, board[i][j]) {
return false
}
}
}
for j := 0; j < 9; j++ {
set := map[byte]bool{}
for i := 0; i < 9; i++ {
if board[i][j] != '.' && !insertIfNotExist(set, board[i][j]) {
return false
}
}
}
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
set := map[byte]bool{}
for m := 0; m < 3; m++ {
for n := 0; n < 3; n++ {
row := i*3 + m
col := j*3 + n
if board[row][col] != '.' && !insertIfNotExist(set, board[row][col]) {
return false
}
}
}
}
}
return true
}