Simple Java Solution, beats 88 % solutions

• ``````public class Solution {
public void solveSudoku(char[][] board) {
solveSudoku(board, 0, 0);
}

public boolean solveSudoku(char[][] board, int row, int col) {
if (row == 9) return true;
int next_row = col == 8 ? row + 1 : row;
int next_col = col == 8 ? 0 : col + 1;

if (board[row][col] != '.') {
return solveSudoku(board, next_row, next_col);
}

for (int i = 1; i <= 9; i++) {
if (check(board, row, col, i) == true) {
board[row][col] = (char)('0' + i);
if (solveSudoku(board, next_row, next_col) == true) return true;
}
}
board[row][col] = '.';
return false;
}

public boolean check(char[][] board, int row, int col, int val) {
char c = (char) ('0' + val);
for (int i = 0; i < 9; i++) {
if (board[row][i] == c && i != col) return false;
if (board[i][col] == c && i != row) return false;
}

int box_row = (row / 3) * 3;
int box_col = (col / 3) * 3;
for (int i = box_row; i < box_row + 3; i++) {
for (int j = box_col; j < box_col + 3; j++) {
if (i != row || j != col) {
if (board[i][j] == c) return false;
}
}
}
return true;
}
}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.