leetcode36

  bool isValidSudoku(vector<vector<char>>& board) {
        const int SIZE = 9;
        for( int row = 0; row < SIZE; row++ ){
            for( int col = 0; col < SIZE; col++ ){
                if( board[row][col] != '.'  && ( !isValidRowAndCol(board, row, col, board[row][col], SIZE) || !isValidRow(board,row,col,board[row][col], SIZE)
                    || !isValidCol(board,row,col,board[row][col], SIZE))){

                   return false;
                }
            }
        }
        return true;
    }
    bool isValidRow(vector<vector<char>>& board, int row, int col, char ch,const int SIZE){
        for(int i=0;iif( i!= col && ch == board[row][i] )
                return false;
        return true;
    }
    bool isValidCol(vector<vector<char>>& board, int row, int col, char ch, const int SIZE){
        for(int i=0;iif( i!=row && ch == board[i][col])
                return false;
        return true;
    }
    bool isValidRowAndCol(vector<vector<char>>& board, int row, int col, char ch, const int SIZE){
        for(int i = row/3*3; i < row/3*3 + 3; i++)
            for(int j = col/3*3; j < col/3*3+3; j++){
                if( ch == board[i][j] && (i!=row || j!=col) )
                    return false;
            }
        return true;
    }

你可能感兴趣的:(leetcode36)