Valid Sudoku

/*
给一个不完整的数独,'.'代表为空判断数独是否合法
*/
public class Solution {
    public boolean check1(char[][] board){
        for(int i = 0 ; i < 9 ; i ++){
            int[] rows = new int[10];
            int[] col = new int[10];
            int[] cube = new int[10];
            for(int j = 0 ; j <9 ; j++){
               if(board[i][j]!='.'){//检查每行
                   if(rows[board[i][j]-'0'] == 1) return false;
                   else  rows[board[i][j]-'0'] = 1;
               }
               if(board[j][i]!='.'){//检查每列
                   if(col[board[j][i]-'0'] == 1) return false;
                   else  col[board[j][i]-'0'] = 1;
               }
               int n = (i/3)*3;
               int m = (i%3)*3;
               if(board[n+j/3][m+j%3]!='.'){//检查每个小正方形
                   if(cube[board[n+j/3][m+j%3]-'0'] == 1) return false;
                   else cube[board[n+j/3][m+j%3]-'0'] = 1;
               }
            }
        }
        return true;
    }
    public boolean isValidSudoku(char[][] board) {
        return check1(board);
    }
}

 

posted @ 2015-04-14 15:05  SprayT  阅读(84)  评论(0编辑  收藏  举报