Valid Sudoku(Easy)

数独,九宫格游戏

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

  • Each row must have the numbers 1-9 occuring just once.
  • Each column must have the numbers 1-9 occuring just once.
  • And the numbers 1-9 must occur just once in each of the 9 sub-boxes of the grid.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

public boolean isValidSudoku(char[][] board) {
    for(int i=0;i<9;i++){
        int[] set1=new int[10];
        int[] set2=new int[10];
        int[] set3=new int[10];
        for(int j=0;j<9;j++){
            //System.out.println(i+","+j);

            char v=board[i][j];
            //System.out.print(v+" ");
            if(v != '.'){
                if(set1[v-'0'] == 1)
                    return false;
                set1[v-'0']=1;
            }
            v=board[j][i];
            //System.out.print(v+" ");
            if(v != '.'){
                if(set2[v-'0'] == 1)
                    return false;
                set2[v-'0']=1;
            }
            v=board[3*(i%3)+j/3][3*(i/3)+j%3];
            //System.out.print(v+"\n");
            if(v != '.'){
                if(set3[v-'0']==1)
                    return false;
                set3[v-'0']=1;
            }
        }
    }
    return true;

}

results matching ""

    No results matching ""