lintcode-easy-Valid Sudoku

Determine whether a Sudoku is valid.

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

 

The following partially filed sudoku is valid.

Valid Sudoku

class Solution {
    /**
      * @param board: the board
        @return: wether the Sudoku is valid
      */
    public boolean isValidSudoku(char[][] board) {
        
        for(int i = 0; i < 9; i++){
                HashSet<Character> row = new HashSet<Character>();
                HashSet<Character> col = new HashSet<Character>();
                HashSet<Character> sqr = new HashSet<Character>();
                
            for(int j = 0; j < 9; j++){
                if(board[i][j] != '.'){
                    if(!row.contains(board[i][j]))
                        row.add(board[i][j]);
                    else
                        return false;
                }
                
                if(board[j][i] != '.'){
                    if(!col.contains(board[j][i]))
                        col.add(board[j][i]);
                    else
                        return false;
                }
                
                if(board[j % 3 + i % 3 * 3][j / 3 + i /3 * 3] != '.'){
                    if(!sqr.contains(board[j % 3 + i % 3 * 3][j / 3 + i /3 * 3]))
                        sqr.add(board[j % 3 + i % 3 * 3][j / 3 + i /3 * 3]);
                    else
                        return false;
                }
                
            }
        }
        
        return true;
    }
};

 

posted @ 2016-03-11 06:57  哥布林工程师  阅读(129)  评论(0编辑  收藏  举报