Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2f63797

Browse files
Adding solution of Valid sudoku
1 parent 31c7287 commit 2f63797

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

‎1-100q/36.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'''
2+
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
3+
4+
Each row must contain the digits 1-9 without repetition.
5+
Each column must contain the digits 1-9 without repetition.
6+
Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.
7+
8+
A partially filled sudoku which is valid.
9+
10+
The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
11+
12+
Example 1:
13+
14+
Input:
15+
[
16+
["5","3",".",".","7",".",".",".","."],
17+
["6",".",".","1","9","5",".",".","."],
18+
[".","9","8",".",".",".",".","6","."],
19+
["8",".",".",".","6",".",".",".","3"],
20+
["4",".",".","8",".","3",".",".","1"],
21+
["7",".",".",".","2",".",".",".","6"],
22+
[".","6",".",".",".",".","2","8","."],
23+
[".",".",".","4","1","9",".",".","5"],
24+
[".",".",".",".","8",".",".","7","9"]
25+
]
26+
Output: true
27+
Example 2:
28+
29+
Input:
30+
[
31+
["8","3",".",".","7",".",".",".","."],
32+
["6",".",".","1","9","5",".",".","."],
33+
[".","9","8",".",".",".",".","6","."],
34+
["8",".",".",".","6",".",".",".","3"],
35+
["4",".",".","8",".","3",".",".","1"],
36+
["7",".",".",".","2",".",".",".","6"],
37+
[".","6",".",".",".",".","2","8","."],
38+
[".",".",".","4","1","9",".",".","5"],
39+
[".",".",".",".","8",".",".","7","9"]
40+
]
41+
Output: false
42+
Explanation: Same as Example 1, except with the 5 in the top left corner being
43+
modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
44+
Note:
45+
46+
A Sudoku board (partially filled) could be valid but is not necessarily solvable.
47+
Only the filled cells need to be validated according to the mentioned rules.
48+
The given board contain only digits 1-9 and the character '.'.
49+
The given board size is always 9x9.
50+
'''
51+
52+
class Solution(object):
53+
def isValidSudoku(self, board):
54+
"""
55+
:type board: List[List[str]]
56+
:rtype: bool
57+
"""
58+
import collections
59+
dict_row, dict_col, dict_cell = collections.defaultdict(set), collections.defaultdict(set), collections.defaultdict(set)
60+
61+
for row_index in range(1, 4):
62+
for col_index in range(1, 4):
63+
for row in range(3*(row_index-1), 3*row_index):
64+
for col in range(3*(col_index-1), 3*col_index):
65+
cell_data = board[row][col]
66+
if cell_data == '.':
67+
continue
68+
if cell_data in dict_row[row] or cell_data in dict_col[col] or cell_data in dict_cell[(row_index, col_index)]:
69+
return False
70+
71+
dict_row[row].add(cell_data)
72+
dict_col[col].add(cell_data)
73+
dict_cell[(row_index, col_index)].add(cell_data)
74+
75+
return True

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
241241
|40|[Combination Sum II](https://leetcode.com/problems/combination-sum-ii/)| [Python](./1-100q/40.py)|Medium|
242242
|39|[Combination Sum](https://leetcode.com/problems/combination-sum/)| [Python](./1-100q/39.py)|Medium|
243243
|38|[Count and Say](https://leetcode.com/problems/count-and-say/)| [Python](./1-100q/38.py)|Easy|
244+
|36|[Valid Sudoku](https://leetcode.com/problems/valid-sudoku)|[Python](./1-100q/36.py)|Medium|
244245
|34|[Search for a Range](https://leetcode.com/problems/search-for-a-range/)| [Python](./1-100q/34.py)|Medium|
245246
|33|[Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)| [Python](./1-100q/33.py)|Hard|
246247
|32|[Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/)| [Python](./1-100q/32.py)|Hard|

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /