-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path036_Valid_Sudoku.py
63 lines (57 loc) · 2.13 KB
/
036_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
# A method from LeetCode
def isValidSudoku(self, board):
return 1 == max(collections.Counter(
x
for i, row in enumerate(board)
for j, c in enumerate(row)
if c != '.'
for x in ((c, i), (j, c), (i/3, j/3, c))
).values() + [1])
#Another method from LeetCode
def isValidSudoku(self, board):
seen = sum(([(c, i), (j, c), (i/3, j/3, c)]
for i, row in enumerate(board)
for j, c in enumerate(row)
if c != '.'), [])
return len(seen) == len(set(seen))
def isValidSudoku(self, board):
seen = sum(([(c, i), (j, c), (i/3, j/3, c)]
for i in range(9) for j in range(9)
for c in [board[i][j]] if c != '.'), [])
return len(seen) == len(set(seen))
def isValidSudoku(self, board):
seen = set()
return not any(x in seen or seen.add(x)
for i, row in enumerate(board)
for j, c in enumerate(row)
if c != '.'
for x in ((c, i), (j, c), (i/3, j/3, c)))
#91.24%
class Solution(object):
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
rowSize = 9
colSize = 9
squareSize = 9
squareRowSize = 3
squareColSize = 3
#Can not use [[]] * 9 here
flagRows = [[], [], [], [], [], [], [], [], []]
flagCols = [[], [], [], [], [], [], [], [], []]
flagSquares = [[], [], [], [], [], [], [], [], []]
for i in range(rowSize):
for j in range(colSize):
val = board[i][j]
if val != '.':
#val = board[i][j]
k = i / squareRowSize * squareColSize + j / squareColSize
if val in flagRows[i] or val in flagCols[j] or val in flagSquares[k]:
return False
else:
flagRows[i].append(val)
flagCols[j].append(val)
flagSquares[k].append(val)
return True