首页 > 其他 > 详细

lintcode-easy-Valid Sudoku

时间:2016-03-11 07:45:48      阅读:118      评论:0      收藏:0      [点我收藏+]

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.

技术分享

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;
    }
};

 

lintcode-easy-Valid Sudoku

原文:http://www.cnblogs.com/goblinengineer/p/5264183.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!