My short solution without using array [C++]


  • 1
    B
    class Solution {
    public:
        bool isValidSudoku(vector<vector<char>>& board) {
            short col, row, sub, c, r, s;
            for(int i(0); i < 9; ++i)
            {
                col = row = sub = 0;
                for(int j(0); j < 9; ++j)
                {
                    c = board[i][j] - '0';
                    r = board[j][i] - '0';
                    s = board[i / 3 * 3 + j / 3][(i % 3) * 3 + j % 3] - '0';
                    
                    if((c = 1 << c) && (c & row) || (row |= c) && 0 ||
                       (r = 1 << r) && (r & col) || (col |= r) && 0 ||
                       (s = 1 << s) && (s & sub) || (sub |= s) && 0) return false;
                }
            }
            return true;
        }
    };
    

    This solution is similar to high vote one, but i'm trying to reduce the memory used.
    I noticed that it's possible to visit a sub-region by "i" and "j" in place.

    In general, my solution is just for fun(Actually, this solution visit all board's cells three times.)


Log in to reply
 

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