星期一, 五月 20, 2013

Valid Sudoku

class Solution {
public:
    bool isValidSudoku(vector<vector > &board) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<vector > rows(9, vector(9, false));
        vector<vector > cols(9, vector(9, false));
        vector<vector > blocks(9, vector(9, false));

        for (int i = 0; i < 9; ++i) {
            for (int j = 0; j < 9; ++j) {
                if (board[i][j] == '.') continue;
                int c = board[i][j] - '1';
                if (rows[i][c] || cols[j][c] || blocks[i - i % 3 + j / 3][c])
                    return false;
                rows[i][c] = cols[j][c] = blocks[i - i % 3 + j / 3][c] = true;
            }
        }
        return true;
    }
};

没有评论:

发表评论