LeetCode刷题 36 有效的数独

 

 

 

 思路:模拟

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        int a[10];
        for(int i = 0; i < board.size(); ++i) {
            memset(a, 0, sizeof a);
            for(int j = 0; j < board[0].size(); ++j) {
                if(board[i][j] != '.') {
                    a[board[i][j] - '0']++;
                    if(a[board[i][j] - '0'] > 1) {
                        return false;
                    }
                }
            }
        }
        for(int i = 0; i < board.size(); ++i) {
            memset(a, 0, sizeof a);
            for(int j = 0; j < board[0].size(); ++j) {
                if(board[j][i] != '.') {
                    a[board[j][i] - '0']++;
                    if(a[board[j][i] - '0'] > 1) {
                        return false;
                    }
                }
            }
        }
        for(int k = 0; k < 9; k += 3) {
            for(int m = 0; m < 9; m += 3) {
                memset(a, 0, sizeof a);
                for(int i = k; i < k + 3; ++i) {
                    for(int j = m; j < m + 3; ++j) {
                        if(board[j][i] != '.') {
                            a[board[j][i] - '0']++;
                            if(a[board[j][i] - '0'] > 1) {
                                return false;
                            }
                        }
                    }
                }
            }
        }
        return true;
    }
};

 

posted @ 2020-04-16 21:54  LightAc  阅读(188)  评论(0编辑  收藏  举报
返回顶端