-
Notifications
You must be signed in to change notification settings - Fork 0
/
q36_Valid_Sudoku.py
50 lines (47 loc) · 2.13 KB
/
q36_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
class Solution:
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
validSet = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}
line = [[] for _ in range(9)]
row = [[] for _ in range(9)]
box = [[] for _ in range(9)]
for x in range(9):
for y in range(9):
data = board[x][y]
z = x // 3 + 3 * (y // 3)
if data == '.':
pass
elif data not in validSet:
return False
elif data in line[x] or data in row[y] or data in box[z]:
return False
else:
line[x].append(data)
row[y].append(data)
box[z].append(data)
return True
def isValidSudoku1(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
seen = []
for i, row in enumerate(board):
for j, c in enumerate(row):
if c != '.':
seen += (j,c),(i,c),(i//3,j//3,c),
return len(seen) == len(set(seen))
board1 = [[".", "8", "7", "6", "5", "4", "3", "2", "1"], ["2", ".", ".", ".", ".", ".", ".", ".", "."],
["3", ".", ".", ".", ".", ".", ".", ".", "."], ["4", ".", ".", ".", ".", ".", ".", ".", "."],
["5", ".", ".", ".", ".", ".", ".", ".", "."], ["6", ".", ".", ".", ".", ".", ".", ".", "."],
["7", ".", ".", ".", ".", ".", ".", ".", "."], ["8", ".", ".", ".", ".", ".", ".", ".", "."],
["9", ".", ".", ".", ".", ".", ".", ".", "."]]
board2 = [[".", ".", "4", ".", ".", ".", "6", "3", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."],
["5", ".", ".", ".", ".", ".", ".", "9", "."], [".", ".", ".", "5", "6", ".", ".", ".", "."],
["4", ".", "3", ".", ".", ".", ".", ".", "1"], [".", ".", ".", "7", ".", ".", ".", ".", "."],
[".", ".", ".", "5", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."],
[".", ".", ".", ".", ".", ".", ".", ".", "."]]
print(Solution().isValidSudoku1(board2))