-
Notifications
You must be signed in to change notification settings - Fork 0
/
036ValidSudoku.py
49 lines (48 loc) · 1.55 KB
/
036ValidSudoku.py
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
class Solution(object):
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
entrys = []
for pos, cur_entry in enumerate(board):
entrys.append([])
for val in cur_entry:
if val == '.':
entrys[pos].append(0)
elif val in entrys[pos]:
return False
else:
entrys[pos].append(val)
m_len = len(entrys)
# compare cur col entry
compare_nums = []
for col in range(m_len):
compare_nums = []
for pos in range(m_len):
if entrys[pos][col] == 0:
continue
elif entrys[pos][col] not in compare_nums:
compare_nums.append(entrys[pos][col])
else:
return False
# compare nine square
x, y = 0, 0
for _ in range(3):
for _ in range(3):
compare_nums = []
m, n = x, y
for i in range(3):
m = x + i
for j in range(3):
n = y + j
if entrys[m][n] == 0:
continue
elif entrys[m][n] not in compare_nums:
compare_nums.append(entrys[m][n])
else:
return False
y += 3
x += 3
y = 0
return True