My short C++ solution!


  • 0
    P
    int primes[10] = {1,2,3,5,7,11,13,17,19,23};
    
    bool check(char c, int &valid) {
        if (c != '.') {
            if (valid % primes[c-'0'] == 0) return false;
            valid *= primes[c-'0'];
        }
        return true;
    }
    
    bool isValidSudoku(vector<vector<char>>& board) {
        for (int x = 0;x < 9;++x) {
            int valid1 = 1, valid2 = 1, valid3 = 1;
            for (int y = 0;y < 9;++y)
                if (!check(board[x][y],valid1) ||!check(board[y][x],valid2) || !check(board[x/3*3+y/3][x%3*3+y%3],valid3))
                    return false;
        }
        return true;
    }

Log in to reply
 

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