Valid Sudoku-merge three check together.


  • 0
    A

    merge col, raw and cube check together.

    class Solution {
    public:
    
    Solution():m1(10,false), m2(10,false), m3(10,false){};
    
    void map_clean(void){
        for (int i=0; i<10; i++){
            m1[i]=m2[i]=m3[i]=false;
        }
    }
    
    bool test_and_set(char c,vector<bool> &m){
        if (c == '.'){
            return true;
        }
        int i = c-'0';
        if (m[i]){
            return false;
        } else {
            m[i]=true;
            return true;
        }
    }
    bool isValidSudoku(vector<vector<char>>& board) {
        int i, j;
    
        for (i=0;i<9;i++){
            map_clean();
            for (j=0; j<9; j++){
                if (!test_and_set(board[i][j], m1)){
                    return false;
                }
                if (!test_and_set(board[j][i], m2)){
                    return false;
                } 
                if (!test_and_set(board[(i%3)*3 + j/3][(i/3)*3 + j%3], m3)){
                    return false;
                }
            }
        }
        return true;
    }
    vector<bool> m1;
    vector<bool> m2;
    vector<bool> m3;
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.