Posted on

Description

Submission

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

		

Leave a Reply

Your email address will not be published. Required fields are marked *