Skip to content

Commit 55a826f

Browse files
committed
update 036
1 parent 9f29d25 commit 55a826f

File tree

3 files changed

+70
-21
lines changed

3 files changed

+70
-21
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
| 66 | [Plus One][066] |
2525
| 283 | [Move Zeroes][283] |
2626
| 1 | [Two Sum][001] |
27+
| 36 | [Valid Sudoku][036] |
2728

2829
**其他**
2930

@@ -47,3 +48,4 @@
4748
[066]: https://github.com/andavid/leetcode-java/blob/master/note/066/README.md
4849
[283]: https://github.com/andavid/leetcode-java/blob/master/note/283/README.md
4950
[001]: https://github.com/andavid/leetcode-java/blob/master/note/001/README.md
51+
[036]: https://github.com/andavid/leetcode-java/blob/master/note/036/README.md

note/036/README.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# [Valid Sudoku][title]
2+
3+
## Description
4+
5+
Determine if a Sudoku is valid, according to: [Sudoku Puzzles - The Rules][sudoku].
6+
7+
The Sudoku board could be partially filled, where empty cells are filled with the character `'.'`.
8+
9+
![](https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
10+
11+
A partially filled sudoku which is valid.
12+
13+
**Note:**
14+
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
15+
16+
## 思路
17+
每个数字,在每一行、每一列、每一个小九宫格内,只能出现一次。
18+
19+
使用 3 个 9*9 的数组,记录每一行、每一列、每一个小九宫格里 1-9 出现的情况,如果某个数字出现,该将数字对应的位置置为 true。
20+
21+
因此,只需要从左往右,从上到下,依次遍历 board 里的数字,一旦发现数字所在的行、列或小九宫格里该数字对应的位置为 true,说明不是有效的数独。
22+
23+
## [完整代码][src]
24+
25+
```java
26+
import java.util.HashSet;
27+
28+
public class Solution {
29+
public boolean isValidSudoku(char[][] board) {
30+
if (board == null || board.length != 9 || board[0].length != 9) {
31+
return false;
32+
}
33+
34+
boolean[][] rows = new boolean[9][9];
35+
boolean[][] cols = new boolean[9][9];
36+
boolean[][] boxs = new boolean[9][9];
37+
38+
for (int i = 0; i < 9; i++) {
39+
for (int j = 0; j < 9; j++) {
40+
if (board[i][j] != '.') {
41+
int k = (i / 3) * 3 + j / 3;
42+
int number = board[i][j] - '0' - 1;
43+
if (rows[i][number] || cols[j][number] || boxs[k][number]) {
44+
return false;
45+
}
46+
rows[i][number] = cols[j][number] = boxs[k][number] = true;
47+
}
48+
}
49+
}
50+
51+
return true;
52+
}
53+
}
54+
```
55+
56+
[title]: https://leetcode.com/problems/valid-sudoku
57+
[sudoku]: http://sudoku.com.au/TheRules.aspx
58+
[src]: https://github.com/andavid/leetcode-java/blob/master/src/com/andavid/leetcode/_036/Solution.java

src/com/andavid/leetcode/_036/Solution.java

+10-21
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,23 @@
22

33
class Solution {
44
public boolean isValidSudoku(char[][] board) {
5-
final int size = 9;
6-
7-
if (board == null || board.length != size || board[0].length != size) {
5+
if (board == null || board.length != 9 || board[0].length != 9) {
86
return false;
97
}
108

11-
ArrayList<HashSet<Character>> row = new ArrayList<HashSet<Character>>();
12-
ArrayList<HashSet<Character>> col = new ArrayList<HashSet<Character>>();
13-
ArrayList<HashSet<Character>> area = new ArrayList<HashSet<Character>>();
14-
15-
for (int i = 0; i < size; i++) {
16-
row.add(new HashSet<Character>());
17-
col.add(new HashSet<Character>());
18-
area.add(new HashSet<Character>());
19-
}
9+
boolean[][] rows = new boolean[9][9];
10+
boolean[][] cols = new boolean[9][9];
11+
boolean[][] boxs = new boolean[9][9];
2012

21-
for (int i = 0; i < size; i++) { // row
22-
for (int j = 0; j < size; j++) { // column
23-
int box = 3 * (i / 3) + j / 3; // sub box
13+
for (int i = 0; i < 9; i++) {
14+
for (int j = 0; j < 9; j++) {
2415
if (board[i][j] != '.') {
25-
if (row.get(i).contains(board[i][j])
26-
|| col.get(j).contains(board[i][j])
27-
|| area.get(box).contains(board[i][j])) {
16+
int k = (i / 3) * 3 + j / 3;
17+
int number = board[i][j] - '0' - 1;
18+
if (rows[i][number] || cols[j][number] || boxs[k][number]) {
2819
return false;
2920
}
30-
row.get(i).add(board[i][j]);
31-
col.get(j).add(board[i][j]);
32-
area.get(box).add(board[i][j]);
21+
rows[i][number] = cols[j][number] = boxs[k][number] = true;
3322
}
3423
}
3524
}

0 commit comments

Comments
 (0)