-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path36.Valid-Sudoku.py
44 lines (39 loc) · 1.35 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
# https://leetcode.com/problems/valid-sudoku/
#
# algorithms
# Medium (43.66%)
# Total Accepted: 246,867
# Total Submissions: 565,463
# beats 74.62% of python submissions
class Solution(object):
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
for i in xrange(9):
is_visited = [False] * 9
for j in xrange(9):
if board[i][j] == '.':
continue
if is_visited[int(board[i][j]) - 1]:
return False
is_visited[int(board[i][j]) - 1] = True
is_visited = [False] * 9
for j in xrange(9):
if board[j][i] == '.':
continue
if is_visited[int(board[j][i]) - 1]:
return False
is_visited[int(board[j][i]) - 1] = True
for i in xrange(3):
for j in xrange(3):
is_visited = [False] * 9
for k in xrange(i * 3, i * 3 + 3):
for l in xrange(j * 3, j * 3 + 3):
if board[k][l] == '.':
continue
if is_visited[int(board[k][l]) - 1]:
return False
is_visited[int(board[k][l]) - 1] = True
return True