Share my cpp solution


  • 0
    B
    class Solution 
    {
    private:
    bool used[10];
    void clear()
    {
        for(int i=1;i<=9;i++)
        {
            used[i]=false;
        }
    }
    public:
    bool isValidSudoku(vector<vector<char>>& board) 
    {
        for(int x=0;x<9;x++)
        {
            clear();
            for(int y=0;y<9;y++)
            {
                char c=board[x][y];
                if(c=='.')continue;
                c-='0';
                if(used[c])return false;
                else used[c]=true;
            }
        }
        for(int y=0;y<9;y++)
        {
            clear();
            for(int x=0;x<9;x++)
            {
                char c=board[x][y];
                if(c=='.')continue;
                c-='0';
                if(used[c])return false;
                else used[c]=true;
            }
        }
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                clear();
                for(int m=0;m<3;m++)
                {
                    for(int n=0;n<3;n++)
                    {
                        int x=i*3+m;
                        int y=j*3+n;
                        char c=board[x][y];
                        if(c=='.')continue;
                        c-='0';
                        if(used[c])return false;
                        else used[c]=true;
                    }
                }
            }
        }
        return true;
    }
    };

Log in to reply
 

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