8000 Total Runtime: 161 ms · michaelhuang/LintCode_Python@ee2d1de · GitHub
[go: up one dir, main page]

Skip to content

Commit ee2d1de

Browse filesBrowse files
committed
1 parent 59269c3 commit ee2d1de

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Valid_Sudoku.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
# @param board, a 9x9 2D array
3+
# @return a boolean
4+
def isValidSudoku(self, board):
5+
# check rows
6+
for i in xrange(9):
7+
if board[i].count('.') + len(set(board[i])) - 1 != 9:
8+
return False
9+
# check columns
10+
for i in xrange(9):
11+
col = [board[j][i] for j in xrange(9)]
12+
if col.count('.') + len(set(col)) - 1 != 9:
13+
return False
14+
# check 3x3 squares
15+
for i in (0, 3, 6):
16+
for j in (0, 3, 6):
17+
square = [board[i + m][j + n]
18+
for m in (0, 1, 2)
19+
for n in (0, 1, 2)]
20+
if square.count('.') + len(set(square)) - 1 != 9:
21+
return False
22+
return True

0 commit comments

Comments
 (0)
0