Leetcode 36. 有效的数独

按题意,暴力解决

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        bool hang[9][9] = {}, lie[9][9] = {}, ge[9][9] = {};
        char x;
        for (int i = 0; i < 9; ++i) {
            for (int j = 0; j < 9; ++j)
                if ((x = board[i][j]) != '.') {
                    if (hang[i][x - '1']) return false;
                    else hang[i][x - '1'] = true;
                    if (lie[j][x - '1']) return false;
                    else lie[j][x - '1'] = true;
                    int k = i / 3 * 3 + j / 3;
                    if (ge[k][x - '1']) return false;
                    else ge[k][x - '1'] = true;
                }
        }
        return true;
    }
};

你可能感兴趣的:(Leetcode 36. 有效的数独)