Skip to content

Commit 0416fb6

Browse files
committed
36. Valid Sudoku solution 2
1 parent 11e3a28 commit 0416fb6

File tree

2 files changed

+96
-45
lines changed

2 files changed

+96
-45
lines changed

src/leetcode/_36_/Solution.java

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,23 @@
11
package leetcode._36_;
22

3-
import java.util.ArrayList;
43
import java.util.HashSet;
5-
import java.util.List;
64
import java.util.Set;
75

86
class Solution {
97
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])) {
8+
Set<String> seen = new HashSet<>();
9+
for (int i = 0; i < 9; i++) {
10+
for (int j = 0; j < 9; j++) {
11+
char num = board[i][j];
12+
if (num != '.') {
13+
if (!seen.add(num + " in row " + i)
14+
|| !seen.add(num + " in column " + j)
15+
|| !seen.add(num + " in block " + i / 3 + "," + j / 3)) {
2216
return false;
2317
}
24-
hList.get(hIndex).add(board[i][j]);
25-
vList.get(vIndex).add(board[i][j]);
2618
}
2719
}
2820
}
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-
}
5821
return true;
5922
}
6023
}

src/leetcode/_36_/solution2.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
### [36\. Valid SudokuCopy for MarkdownCopy for MarkdownCopy 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+
class Solution {
69+
   public boolean isValidSudoku(char[][] board) {
70+
       Set<String> seen = new HashSet<>();
71+
       for (int i = 0; i < 9; i++) {
72+
           for (int j = 0; j < 9; j++) {
73+
               char num = board[i][j];
74+
               if (num != '.') {
75+
                   if (!seen.add(num + " in row " + i)
76+
                           || !seen.add(num + " in column " + j)
77+
                           || !seen.add(num + " in block " + i / 3 + "," + j / 3)) {
78+
                       return false;
79+
                  }
80+
              }
81+
          }
82+
      }
83+
       return true;
84+
  }
85+
}
86+
87+
```
88+
![](https://ws1.sinaimg.cn/large/006tKfTcgy1g1ajv1n01vj30zo0lc77f.jpg)

0 commit comments

Comments
 (0)