[LeetCode 36] Valid Sudoku Solution

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

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


A partially filled sudoku which is valid.

Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

Ideas: first of all, we need to know what is sudoku board, we could check the blog: Sudoku Puzzles - 九宫格(数独)游戏

Then, whether the soduku board is valid or not based on the following rules.

1) The width and length of the board should 9;

2) In the one row, there is only distinct number (between 1 to 9) or '.'; 

3) In one column, there is only distinct number (between 1 to 9) or '.'; 

4) In small 3 * 3 board, it should only contain the distinct number(between 1 to 9) or '.'; 

The tricks are as followings:(1) how to use vector, (2) use a vector to flag whether the digit are distinct. (3) how to get the small 3*3 subbox.

class Solution {
public:
    bool isValidSudoku(vector<vector<char> > &board) {
        if(board.size() != 9) return false;
        
        //row is valid, column is valid, small board is valid ?
        
        //row is valid
        for(int r = 0; r < 9; r++){
            vector<bool> flag (9, false);
            for(int c = 0; c < 9; c++){
                if(board[r][c] == '.') continue;
                
                if(isDigit(board[r][c])){
                    if(flag[(board[r][c] - '0')])
                        return false;
                    flag[(board[r][c] - '0')] = true;
                }
            }
        }
        
        // column is valid?
        for(int c = 0; c < 9; c++){
            vector<bool> flag (9, false);
            for(int r = 0; r < 9; r++){
                if(board[r][c] == '.') continue;
                
                if(isDigit(board[r][c])){
                    if(flag[(board[r][c] - '0')])
                        return false;
                    flag[(board[r][c] - '0')] = true;
                }
            }
        }
        
        //small 3 * 3 is valid?
        for(int r = 0; r < 9; r += 3){
            for(int c = 0; c < 9; c += 3){
                //how to set in a 3 * 3 sub-board
                vector<bool> flag (9, false);
                for(int i = 0; i < 3; i++){
                    for(int j = 0; j < 3; j++){
                        if(board[r + i][c + j] == '.') continue;
                        
                        if(isDigit(board[r + i][c + j])){
                            if(flag[(board[r + i][c + j] - '0')])
                                return false;
                            flag[(board[r + i][c + j] - '0')] = true;
                        }
                    }
                }
            }
        }
        
        return true;
        
    }
private:
    bool isDigit(char i){
        if (i - '0' < 10 && i - '0' >0) 
            return true ;
        else 
            return false;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值