Very short C++ code using only one loop


  • 2
    M
    int a[9];
    class Solution {
    public:
        bool isValidSudoku(vector<vector<char>>& board) {
            memset(a,0,sizeof(a));
            for (int i=0,j=0,row=0,col=0;i<9;++j,j==9?++i,j=0,row=col=0:0)
            {
                if (board[i][j]!='.')
                {
                    if ((1<<board[i][j]-48) & row) return false;
                    else row|=1<<board[i][j]-48;
                    if ((1<<board[i][j]-48) & a[i/3*3+j/3]) return false;
                    else a[i/3*3+j/3]|=1<<board[i][j]-48;
                }
                if (board[j][i]!='.')
                    if ((1<<board[j][i]-48) & col) return false;
                    else col|=1<<board[j][i]-48;
            }
            return true;
        }
    };

  • -10
    J

    HeHe, you are too young too arrogant


  • 0
    T

    It's not a code golf / minification contest.

    There are some good points in your code, but those would remain if you formatted and de-magified it.


Log in to reply
 

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