[36]Valid Sudoku

来源:互联网 发布:由诲女知之乎的意思 编辑:程序博客网 时间:2024/06/05 04:56

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.


A partially filled sudoku which is valid.

class Solution {public:    bool isValidSudoku(vector<vector<char> > &board) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        vector<vector<bool> > rows(9, vector<bool>(9, false));        vector<vector<bool> > cols(9, vector<bool>(9, false));        vector<vector<bool> > blocks(9, vector<bool>(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/3*3 + j/3][c])                    return false;                rows[i][c] = cols[j][c] = blocks[i/3*3 + j/3][c] = true;            }        }        return true;    }};


原创粉丝点击