可以只loop一遍
https://discuss.leetcode.com/topic/9748/shared-my-concise-java-code
public boolean isValidSudoku(char[][] board) {
for(int i = 0; i<9; i++){
HashSet<Character> rows = new HashSet<Character>();
HashSet<Character> columns = new HashSet<Character>();
HashSet<Character> cube = new HashSet<Character>();
for (int j = 0; j < 9;j++){
if(board[i][j]!='.' && !rows.add(board[i][j]))
return false;
if(board[j][i]!='.' && !columns.add(board[j][i]))
return false;
int RowIndex = 3*(i/3)+j/3;
int ColIndex = 3*(i%3)+j%3;
if(board[RowIndex][ColIndex]!='.' && !cube.add(board[RowIndex ][ColIndex]))
return false;
}
}
return true;
}
没有评论:
发表评论