LeetCode36.有效的数独

请你判断一个 9 x 9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。

数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
 

注意:

一个有效的数独(部分已被填充)不一定是可解的。
只需要根据以上规则,验证已经填入的数字是否有效即可。
空白格用 '.' 表示。

示例 1:
输入:board = 
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
输出:true

思路1:暴力模拟

代码如下:

class Solution {
public:
    bool isValidSudoku(vector>& board) {
        int c = board.size();
        int r = board[0].size();
        set stc;
        set str;
        set stx;
        for(auto& i : board){
            for(auto& j : i){
                if(j != '.'){
                    if(stc.find(j) == stc.end()){
                        stc.insert(j);
                    }else{
                        return false;
                    }
                }
            }
            stc.clear();
        }
        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++){
                if(board[j][i] != '.'){
                    if(str.find(board[j][i]) == str.end()){
                        str.insert(board[j][i]);
                    }else{
                        return false;
                    }
                }
            }
            str.clear();
        }
        for(int i1 = 0; i1 < c; i1 += 3){
            for(int j1 = 0; j1 < r; j1 += 3){
                for(int i = i1;i < i1 + 3; i++){
                    for(int j = j1; j < j1 + 3; j++){
                        if(board[i][j] != '.'){
                            if(stx.find(board[i][j]) == stx.end()){
                                stx.insert(board[i][j]);
                            }else{
                                return false;
                            }
                        }
                    }
                }
                stx.clear();
            }
        }
        return true;
    }
};

思路2:哈希映射,将非‘.’的char转换成int,作为下标,存储数字就+1,若重复,则对应位置值应当大于等于2

代码如下:

class Solution {
public:
    bool isValidSudoku(vector>& board) {
        int rows[9][9];
        int columns[9][9];
        int subboxes[3][3][9];
        
        memset(rows,0,sizeof(rows));
        memset(columns,0,sizeof(columns));
        memset(subboxes,0,sizeof(subboxes));
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                char c = board[i][j];
                if (c != '.') {
                    int index = c - '0' - 1;
                    rows[i][index]++;
                    columns[j][index]++;
                    subboxes[i / 3][j / 3][index]++;
                    if (rows[i][index] > 1 || columns[j][index] > 1 || subboxes[i / 3][j / 3][index] > 1) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
};

你可能感兴趣的:(leetcode,算法,数据结构,c++)