Skip to content

Commit 74c7d61

Browse files
author
王俊超
committed
commit
1 parent 172502e commit 74c7d61

File tree

9 files changed

+61
-0
lines changed

9 files changed

+61
-0
lines changed

Diff for: .gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
*.iml
3+
.idea
4+

Diff for: .idea/modules.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
-715 Bytes
Binary file not shown.
Binary file not shown.

Diff for: out/production/【013】【3Sum】/Main.class

-666 Bytes
Binary file not shown.

Diff for: out/production/【013】【3Sum】/Solution.class

-1.76 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.

Diff for: 【037】【Sudoku Solver】/src/Solution.java

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @author: wangjunchao(王俊超)
3+
* @time: 2018-09-28 15:06
4+
**/
5+
public class Solution {
6+
public void solveSudoku(char[][] board) {
7+
if(board == null || board.length == 0) {
8+
return;
9+
}
10+
solve(board);
11+
}
12+
13+
public boolean solve(char[][] board){
14+
for(int i = 0; i < board.length; i++){
15+
for(int j = 0; j < board[0].length; j++){
16+
if(board[i][j] == '.'){
17+
// 尝试1到9的数字
18+
for(char c = '1'; c <= '9'; c++){
19+
if(isValid(board, i, j, c)){
20+
// 将字符放入单元格
21+
board[i][j] = c;
22+
23+
// 如果字符可以放入单元格返回true
24+
if(solve(board)){
25+
return true;
26+
}else { // 如果字符不能放入单元格就是还原成点号
27+
board[i][j] = '.';
28+
}
29+
}
30+
}
31+
return false;
32+
}
33+
}
34+
}
35+
return true;
36+
}
37+
38+
private boolean isValid(char[][] board, int row, int col, char c){
39+
for(int i = 0; i < 9; i++) {
40+
// 检查行
41+
if(board[i][col] != '.' && board[i][col] == c) {
42+
return false;
43+
}
44+
// 检查列
45+
if(board[row][i] != '.' && board[row][i] == c) {
46+
return false;
47+
}
48+
// 检查3*3的单元格
49+
if(board[3 * (row / 3) + i / 3][ 3 * (col / 3) + i % 3] != '.'
50+
&& board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == c) {
51+
return false;
52+
}
53+
}
54+
return true;
55+
}
56+
}

0 commit comments

Comments
 (0)