题目链接
https://leetcode.com/problems/valid-sudoku/discuss/
题目描述
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
Each row must contain the digits 1-9 without repetition.
Each column must contain the digits 1-9 without repetition.
Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.
The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’.
Example 1:
Input:
[
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
Output: true
Example 2:
Input:
[
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being
modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
代码一
这是自己最初写的代码,效率比较低,三次for循环,依次判定
class Solution {
public boolean isValidSudoku(char[][] board) {
if(board == null || board.length != 9 || board[0].length != 9) {
throw new RuntimeException();
}
int[] rows = new int[10];
int[] cols = new int[10];
int[] squ = new int[10];
for(int i = 0; i < 9; i++) {
memeryset(cols);
for(int j = 0; j < 9; j++) {
if(board[i][j] != '.' ) {
int num = board[i][j] - '0';
if(cols[num] == 0) {
cols[num] = 1;
}else {
return false;
}
}
}
}
System.out.println("=================");
for(int j = 0; j < 9; j++) {
memeryset(rows);
for(int i = 0; i < 9; i++) {
if(board[i][j] != '.') {
int num = board[i][j] - '0';
if(rows[num] == 0) {
rows[num] = 1;
}else {
return false;
}
}
}
}
System.out.println("++++++++++++++++++++++++++++");
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
memeryset(squ);
for(int m = 0; m < 3; m++) {
for(int n = 0; n < 3; n++) {
char c = board[i*3+m][j*3+n];
if(c != '.') {
int num = c - '0';
if(squ[num] == 0) {
squ[num] = 1;
} else {
return false;
}
}
}
}
}
}
return true;
}
private void memeryset(int[] nums) {
for(int i = 0; i < nums.length; i++) {
nums[i] = 0;
}
}
}
代码二
该方法的可读性比较好,思路如下:
第4行的‘2’表示为4(2)
第4列的‘2’表示为(2)4
右上角的’2’表示为0(2)2
class Solution {
public boolean isValidSudoku(char[][] board) {
if(board == null || board.length != 9 || board[0].length != 9) {
throw new RuntimeException();
}
Set<String> seen = new HashSet<>();
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
if(board[i][j] != '.') {
String b = "(" + board[i][j] + ")";
if(!seen.add(b + i) || !seen.add( j + b) || !seen.add(i/3 + b + j/3) ) {
return false;
}
}
}
}
return true;
}
}
代码三:
该方法用到了位运算,比较巧妙,也比较好懂,在以后的解题过程中,如果用0和1进行判断时,要想到使用位运算,会节省时间和空间。
class Solution {
public boolean isValidSudoku(char[][] board) {
if(board == null || board.length != 9 || board[0].length != 9) {
throw new RuntimeException();
}
int[] rows = new int[9];
int[] cols = new int[9];
int[] squ = new int[9];
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
if(board[i][j] != '.') {
int num = board[i][j] - '1';
int value = ( 1 << num);
if((value & rows[i]) != 0 ) {
return false;
}
if((value & cols[j]) != 0) {
return false;
}
if((value & squ[(i/3)*3 + j/3]) != 0) {
return false;
}
rows[i] |= value;
cols[j] |= value;
squ[(i/3)*3 + j/3] |= value;
}
}
}
return true;
}
}