LeetCode-Valid Sudoku

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.

class Solution {
public:
    bool isValidSudoku(vector<vector<char> > &board) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        bool count[9];
        for(int i=0;i<9;i++){
            memset(count,false,9);
            for(int j=0;j<9;j++){
                int ind=board[i][j]-'1';
                if(ind>=0&&ind<9){
                    if(count[ind])return false;
                    count[ind]=true;
                }
            }
        }
        for(int j=0;j<9;j++){
            memset(count,false,9);
            for(int i=0;i<9;i++){
                int ind=board[i][j]-'1';
                if(ind>=0&&ind<9){
                    if(count[ind])return false;
                    count[ind]=true;
                }
            }
        }
        for(int i=0;i<9;i+=3){
            for(int j=0;j<9;j+=3){
                 memset(count,false,9);
                 for(int m=0;m<3;m++){
                     for(int n=0;n<3;n++){
                        int ind=board[i+m][j+n]-'1';
                        if(ind>=0&&ind<9){
                            if(count[ind])return false;
                            count[ind]=true;
                        }
                     }
                 }
            }
        }
        return true;
    }
};
View Code
原文地址:https://www.cnblogs.com/superzrx/p/3351482.html