Skip to content

Instantly share code, notes, and snippets.

@rayjcwu
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save rayjcwu/9064759 to your computer and use it in GitHub Desktop.

Select an option

Save rayjcwu/9064759 to your computer and use it in GitHub Desktop.
public boolean isValid(char [][]board) {
for (int i = 0; i < 9; i++) {
if (!isValid(board, 0, i, 9, 1) || !isValid(board, i, 0, 1, 9)) {
return false;
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (!isValid(board, 3*i, 3*j, 3, 3)) {
return false;
}
}
}
return true;
}
private boolean isValid(char [][]board, int y, int x, int h, int w) {
boolean []used = new boolean[9];
for (int i = y; i < y + h; i++) {
for (int j = x; j < x + w; j++) {
if (board[i][j] != '.') {
int val = charToInt(board[i][j]);
if (used[val - 1]) {
return false;
} else {
used[val - 1] = true;
}
}
}
}
return true;
}
private int charToInt(char c) {
return c - '0';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment