Skip to content

Commit e6977fc

Browse files
[LEET-37] Sudoko Solver
1 parent 8949bfe commit e6977fc

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@
278278
|40|[Combination Sum II](https://leetcode.com/problems/combination-sum-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/CombinationSumII.java)|O(k*n^k)|O(k)|Medium|Backtracking
279279
|39|[Combination Sum](https://leetcode.com/problems/combination-sum/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/CombinationSum.java)|O(k*n^k)|O(k)|Medium|Backtracking
280280
|38|[Count and Say](https://leetcode.com/problems/count-and-say/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/CountandSay.java)|O(n*2^n)|O(2^n)|Easy| Recursion, LinkedList
281+
|37|[Sudoku Solver](https://leetcode.com/problems/sudoku-solver/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SudokuSolver.java)|O((9!)^9)|O(1)|Hard|
281282
|36|[Valid Sudoku](https://leetcode.com/problems/valid-sudoku/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ValidSudoku.java)|O(?)|O(?)|Medium|
282283
|35|[Search Insert Position](https://leetcode.com/problems/search-insert-position/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SearchInsertPosition.java)|O(n)|O(1)|Medium|Array
283284
|34|[Search for a Range](https://leetcode.com/problems/search-for-a-range/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SearchForARange.java)|O(logn)|O(1)|Medium|Array, Binary Search
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.stevesun.solutions;
2+
3+
/**
4+
* Write a program to solve a Sudoku puzzle by filling the empty cells.
5+
* Empty cells are indicated by the character '.'.
6+
* You may assume that there will be only one unique solution.
7+
* A sudoku puzzle...
8+
* ...and its solution numbers marked in red.
9+
*/
10+
public class SudokuSolver {
11+
12+
public void solveSudoku(char[][] board) {
13+
if (board == null || board.length == 0) return;
14+
solve(board);
15+
}
16+
17+
private boolean solve(char[][] board) {
18+
for (int i = 0; i < board.length; i++) {
19+
for (int j = 0; j < board[0].length; j++) {
20+
if (board[i][j] == '.') {
21+
for (char c = '1'; c <= '9'; c++) {//try 1 to 9
22+
if (isValid(board, i, j, c)) {
23+
board[i][j] = c;
24+
25+
if (solve(board)) return true;
26+
else board[i][j] = '.';//recover it to be '.'
27+
}
28+
}
29+
return false;
30+
}
31+
}
32+
}
33+
return true;
34+
}
35+
36+
private boolean isValid(char[][] board, int row, int col, char c) {
37+
for (int i = 0; i < 9; i++) {
38+
if (board[i][col] != '.' && board[i][col] == c) return false;//check row
39+
if (board[row][i] != '.' && board[row][i] == c) return false;//check column
40+
if (board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] != '.' && board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == c)
41+
return false; //check 3*3 block
42+
}
43+
return true;
44+
}
45+
46+
}

0 commit comments

Comments
 (0)