forked from 617076674/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
65 lines (60 loc) · 1.85 KB
/
Copy pathSolution.java
File metadata and controls
65 lines (60 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package question0037_sudoku_solver;
import java.util.ArrayList;
import java.util.List;
/**
* https://leetcode-cn.com/problems/sudoku-solver/
*
* 回溯法。
*
* 执行用时:9ms,击败64.47%。消耗内存:35.4MB,击败70.78%。
*/
public class Solution {
private List<Integer> list = new ArrayList<>();
public void solveSudoku(char[][] board) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] == '.') {
list.add(i * 10 + j);
}
}
}
try {
fillNumber(board, 0);
} catch (Exception e) {}
}
private void fillNumber(char[][] board, int index) {
if (index == list.size()) {
throw new RuntimeException(); //利用异常来跳出该函数
}
int i = list.get(index) / 10, j = list.get(index) % 10;
for (int num = 1; num <= 9; num++) {
if (canFillThisNum(board, i, j, num)) {
board[i][j] = (char) (num + '0');
fillNumber(board, index + 1);
board[i][j] = '.'; //变量的回溯
}
}
}
private boolean canFillThisNum(char[][] board, int x, int y, int num) {
if (board[x][y] != '.') {
return false;
}
for (int k = 0; k < 9; k++) {
if (num == board[x][k] - '0') {
return false;
}
if (num == board[k][y] - '0') {
return false;
}
}
int top = (x / 3) * 3, bottom = top + 2, left = (y / 3) * 3, right = left + 2;
for (int i = top; i <= bottom; i++) {
for (int j = left; j <= right; j++) {
if (num == board[i][j] - '0') {
return false;
}
}
}
return true;
}
}