-
Notifications
You must be signed in to change notification settings - Fork 0
/
36-三遍遍历-Valid Sudoku.py
95 lines (88 loc) · 2.89 KB
/
36-三遍遍历-Valid Sudoku.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
class Solution:
def isValidSudoku(self, board):
A = set()
for i in range(9):
for item in board[i]: # row
if item == '.':
continue
else:
if item not in A:
A.add(item)
else:
#self.flag = 1
return False
A = set() # 重置set
for i in range(9):
for item in [x[i] for x in board]:
if item == '.':
continue
else:
if item not in A:
A.add(item)
else:
#self.flag = 1
return False
A = set()
def box_of_nine(board,a,b,initial_set):
initial_set = set()
for p in range(3):
for q in range(3):
if board[a+p][b+q] == '.':
continue
else:
if board[a+p][b+q] not in initial_set:
initial_set.add(board[a+p][b+q])
else:
return False
a = [0,3,6]
b = [0,3,6]
for i in a:
for j in b:
Q = box_of_nine(board,i,j,set())
if Q == False:
return False
return True
class Solution:
def __init__(self):
self.flag = 0
def isValidSudoku(self, board):
A = set()
for i in range(9):
for item in board[i]: # row
if item == '.':
continue
else:
if item not in A:
A.add(item)
else:
#self.flag = 1
return False
A = set() # 重置set
for i in range(9):
for item in [x[i] for x in board]:
if item == '.':
continue
else:
if item not in A:
A.add(item)
else:
#self.flag = 1
return False
A = set()
def box_of_nine(board,a,b,initial_set):
initial_set = set()
for p in range(3):
for q in range(3):
if board[a+p][b+q] == '.':
continue
else:
if board[a+p][b+q] not in initial_set:
initial_set.add(board[a+p][b+q])
else:
self.flag = 1
a = [0,3,6]
b = [0,3,6]
for i in a:
for j in b:
box_of_nine(board,i,j,set())
return self.flag == 0