-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path36.py
61 lines (52 loc) · 2 KB
/
36.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
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <[email protected]>']
class Solution(object):
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
row_num = len(board)
col_num = len(board[0])
for i in xrange(0, row_num):
num_set = set()
for j in xrange(0, col_num):
if board[i][j] == '.':
pass
else:
if board[i][j] in num_set or int(board[i][j]) < 1 or int(board[i][j]) > 9:
return False
else:
num_set.add(board[i][j])
if len(num_set) > 9:
return False
for j in xrange(0, col_num):
num_set = set()
for i in xrange(0, row_num):
if board[i][j] == '.':
pass
else:
if board[i][j] in num_set or int(board[i][j]) < 1 or int(board[i][j]) > 9:
return False
else:
num_set.add(board[i][j])
if len(num_set) > 9:
return False
for i in xrange(0, row_num-2, 3):
for j in xrange(0, col_num-2, 3):
num_set = set()
for m in xrange(i, i+3):
for n in xrange(j, j+3):
if board[m][n] == '.':
pass
else:
if board[m][n] in num_set or int(board[m][n]) < 1 or int(board[m][n]) > 9:
return False
else:
num_set.add(board[m][n])
if len(num_set) > 9:
return False
return True
if __name__ == "__main__":
print Solution().isValidSudoku([".87654321","2........","3........","4........","5........","6........","7........","8........","9........"])