[matrix] Clear C++ no need to explain


  • 0
        bool isValidSudoku(vector<vector<char>>& board) {
            for(int i=0;i<board.size();i++){
                unordered_map<char,int>m;
                //check each row
                for(int j=0;j<board[0].size();j++){
                    if(board[i][j]=='.') continue;
                    if(m[board[i][j]]>0) return false;
                    m[board[i][j]]++;
                }
                m.clear();
                //check each column
                for(int j=0;j<board[0].size();j++){
                    if(board[j][i]=='.') continue;
                    if(m[board[j][i]]>0) return false;
                    m[board[j][i]]++;
                }
            }
            //check each sub-boxes
            int r=0, c=0;
            while(r<board.size()){
                if(!isValidSub(board,r,c)) return false;
                c+=3;
                if(c==board[0].size()) r+=3,c=0;
            }
            return true;
        }
        
        bool isValidSub(vector<vector<char>>& board,int r,int c){
            unordered_map<char,int>m;
            for(int i=r;i<r+3;i++){
                for(int j=c;j<c+3;j++){
                    if(board[i][j]=='.') continue;
                    if(m[board[i][j]]>0) return false;
                    m[board[i][j]]++;
                }
            }
            return true;
        }
    

Log in to reply
 

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