-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsol3.py
66 lines (55 loc) · 2.02 KB
/
sol3.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
from typing import List, Optional
class Solution:
SIZE = 9
def isValidSudoku(self, board: List[List[str]]) -> bool:
rows = [{} for _ in range(9)]
cols = [{} for _ in range(9)]
squares = [{} for _ in range(9)]
for row in range(self.SIZE):
for col in range(self.SIZE):
item = board[row][col]
if item == ".":
continue
actual_pos = int(item) - 1
square_pos = self.getSquareByPosition(row, col) - 1
if actual_pos in rows[row]:
return False
if actual_pos in cols[col]:
return False
if actual_pos in squares[square_pos]:
return False
rows[row][actual_pos] = True
cols[col][actual_pos] = True
squares[square_pos][actual_pos] = True
return True
def getSquareByPosition(self, row: int, col: int) -> int:
mult = row // 3
left_sqaure_pos = 3 * mult + 1
horizontal = col // 3
square = left_sqaure_pos + horizontal
return square
board = [
["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]
]
board = [
["8","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]
]
s = Solution()
res = s.isValidSudoku(board)
print(res)