Skip to content

Commit 11e3a28

Browse files
committed
36. Valid Sudoku
1 parent dcb2adb commit 11e3a28

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed

src/leetcode/_36_/Main.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode._36_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
char charArray[][] = {
10+
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
11+
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
12+
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
13+
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
14+
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
15+
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
16+
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
17+
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
18+
{'.', '.', '.', '.', '8', '.', '.', '7', '9'}};
19+
System.out.println(solution.isValidSudoku(charArray));
20+
}
21+
}
22+

src/leetcode/_36_/Solution.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package leetcode._36_;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.Set;
7+
8+
class Solution {
9+
public boolean isValidSudoku(char[][] board) {
10+
List<Set<Character>> hList = new ArrayList<>(27);
11+
List<Set<Character>> vList = new ArrayList<>(27);
12+
for (int i = 0; i < 27; i++) {
13+
hList.add(new HashSet<>());
14+
vList.add(new HashSet<>());
15+
}
16+
for (int i = 0; i < board.length; i++) {
17+
for (int j = 0; j < board[i].length; j++) {
18+
int vIndex = i + j / 3 * 9;
19+
int hIndex = j + i / 3 * 9;
20+
if (board[i][j] != '.') {
21+
if (hList.get(hIndex).contains(board[i][j]) || vList.get(vIndex).contains(board[i][j])) {
22+
return false;
23+
}
24+
hList.get(hIndex).add(board[i][j]);
25+
vList.get(vIndex).add(board[i][j]);
26+
}
27+
}
28+
}
29+
for (int i = 0; i < 9; i++) {
30+
// every row
31+
Set<Character> set = new HashSet<>();
32+
set.addAll(vList.get(i));
33+
set.addAll(vList.get(i + 9));
34+
set.addAll(vList.get(i + 9 * 2));
35+
if (set.size() != (vList.get(i).size() + vList.get(i + 9).size() + vList.get(i + 9 * 2).size())) {
36+
return false;
37+
}
38+
39+
// every columns
40+
Set<Character> set2 = new HashSet<>();
41+
set2.addAll(hList.get(i));
42+
set2.addAll(hList.get(i + 9));
43+
set2.addAll(hList.get(i + 9 * 2));
44+
if (set2.size() != (hList.get(i).size() + hList.get(i + 9).size() + hList.get(i + 9 * 2).size())) {
45+
return false;
46+
}
47+
48+
// every little 9 grips
49+
Set<Character> set3 = new HashSet<>();
50+
set3.addAll(vList.get(i * 3));
51+
set3.addAll(vList.get(i * 3 + 1));
52+
set3.addAll(vList.get(i * 3 + 2));
53+
if (set3.size() != (vList.get(i * 3).size() + vList.get(i * 3 + 1).size() + vList.get(i * 3 + 2).size())) {
54+
return false;
55+
}
56+
57+
}
58+
return true;
59+
}
60+
}

src/leetcode/_36_/solution.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
### [36\. Valid SudokuCopy for Markdown](https://leetcode.com/problems/valid-sudoku/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated **according to the following rules**:
7+
8+
1. Each row must contain the digits `1-9` without repetition.
9+
2. Each column must contain the digits `1-9` without repetition.
10+
3. Each of the 9 `3x3` sub-boxes of the grid must contain the digits `1-9` without repetition.
11+
12+
![](https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
13+
<small style="display: inline;">A partially filled sudoku which is valid.</small>
14+
15+
The Sudoku board could be partially filled, where empty cells are filled with the character `'.'`.
16+
17+
**Example 1:**
18+
19+
```
20+
Input:
21+
[
22+
["5","3",".",".","7",".",".",".","."],
23+
["6",".",".","1","9","5",".",".","."],
24+
[".","9","8",".",".",".",".","6","."],
25+
["8",".",".",".","6",".",".",".","3"],
26+
["4",".",".","8",".","3",".",".","1"],
27+
["7",".",".",".","2",".",".",".","6"],
28+
[".","6",".",".",".",".","2","8","."],
29+
[".",".",".","4","1","9",".",".","5"],
30+
[".",".",".",".","8",".",".","7","9"]
31+
]
32+
Output: true
33+
```
34+
35+
**Example 2:**
36+
37+
```
38+
Input:
39+
[
40+
  ["8","3",".",".","7",".",".",".","."],
41+
  ["6",".",".","1","9","5",".",".","."],
42+
  [".","9","8",".",".",".",".","6","."],
43+
  ["8",".",".",".","6",".",".",".","3"],
44+
  ["4",".",".","8",".","3",".",".","1"],
45+
  ["7",".",".",".","2",".",".",".","6"],
46+
  [".","6",".",".",".",".","2","8","."],
47+
  [".",".",".","4","1","9",".",".","5"],
48+
  [".",".",".",".","8",".",".","7","9"]
49+
]
50+
Output: false
51+
Explanation: Same as Example 1, except with the 5 in the top left corner being
52+
modified to 8\. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
53+
```
54+
55+
**Note:**
56+
57+
* A Sudoku board (partially filled) could be valid but is not necessarily solvable.
58+
* Only the filled cells need to be validated according to the mentioned rules.
59+
* The given board contain only digits `1-9` and the character `'.'`.
60+
* The given board size is always `9x9`.
61+
62+
63+
#### Solution
64+
65+
Language: **Java**
66+
67+
```java
68+
      }
69+
       for (int i = 0; i < 9; i++) {
70+
           // every row
71+
           Set<Character> set = new HashSet<>();
72+
           set.addAll(vList.get(i));
73+
           set.addAll(vList.get(i + 9));
74+
           set.addAll(vList.get(i + 9 * 2));
75+
           if (set.size() != (vList.get(i).size() + vList.get(i + 9).size() + vList.get(i + 9 * 2).size())) {
76+
               return false;
77+
          }
78+
79+
           // every columns
80+
           Set<Character> set2 = new HashSet<>();
81+
           set2.addAll(hList.get(i));
82+
           set2.addAll(hList.get(i + 9));
83+
           set2.addAll(hList.get(i + 9 * 2));
84+
           if (set2.size() != (hList.get(i).size() + hList.get(i + 9).size() + hList.get(i + 9 * 2).size())) {
85+
               return false;
86+
          }
87+
88+
           // every little 9 grips
89+
           Set<Character> set3 = new HashSet<>();
90+
           set3.addAll(vList.get(i * 3));
91+
           set3.addAll(vList.get(i * 3 + 1));
92+
           set3.addAll(vList.get(i * 3 + 2));
93+
           if (set3.size() != (vList.get(i * 3).size() + vList.get(i * 3 + 1).size() + vList.get(i * 3 + 2).size())) {
94+
               return false;
95+
          }
96+
97+
      }
98+
       return true;
99+
  }
100+
}
101+
102+
```
103+
![](https://ws3.sinaimg.cn/large/006tKfTcgy1g1ajb5r7onj30xk0u0gql.jpg)

0 commit comments

Comments
 (0)