• 面试经典150题——Day34


    一、题目

    36. Valid Sudoku

    Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

    Each row must contain the digits 1-9 without repetition.
    Each column must contain the digits 1-9 without repetition.
    Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
    Note:

    A Sudoku board (partially filled) could be valid but is not necessarily solvable.
    Only the filled cells need to be validated according to the mentioned rules.

    Example 1:

    Input: 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”]]
    Output: true
    Example 2:

    Input: board =
    [[“8”,“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”]]
    Output: false
    Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8’s in the top left 3x3 sub-box, it is invalid.

    Constraints:

    board.length == 9
    board[i].length == 9
    board[i][j] is a digit 1-9 or ‘.’.

    题目来源: leetcode

    二、题解

    我自己的笨方法

    class Solution {
    public:
        bool isValidSudoku(vector<vector<char>>& board) {
            unordered_map<char,int> map;
            //遍历行
            for(int i = 0;i < 9;i++){
                //清除上行数据
                map.clear();
                for(int j = 0;j < 9;j++){
                    char c = board[i][j];
                    if(c != '.'){
                        if(map[c] == 0) map[c]++;
                        else if(map[c] == 1) return false;
                    }
                }
            }
            //遍历列
            for(int j = 0;j < 9;j++){
                map.clear();
                for(int i = 0;i < 9;i++){
                    char c = board[i][j];
                    if(c != '.'){
                        if(map[c] == 0) map[c]++;
                        else if(map[c] == 1) return false;
                    }
                }
            }
            //遍历3x3宫格
            for(int i = 0;i < 9;i += 3){
                for(int j = 0;j < 9;j += 3){
                    map.clear();
                    for(int k = i;k < i + 3;k++){
                        for(int m = j;m < j + 3;m++){
                            char c = board[k][m];
                            if(c != '.'){
                                if(map[c] == 0) map[c]++;
                                else if(map[c] == 1) return false;
                            }
                        }
                    }
                }
            }
            return true;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    答案的好方法

    class Solution {
    public:
        bool isValidSudoku(vector<vector<char>>& 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;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  • 相关阅读:
    Qt-OpenCV学习笔记--自适应阈值--adaptiveThreshold()
    【jQuery Demo】图片瀑布流实现
    HttpRunner--安装使用
    MongoDB打破了原则引入SQL?
    第三章 解析python中opencv,matpoltlib与pillow对JPG和PNG读取和写入(工具)
    推荐一款功能强大的 GPT 学术优化开源项目GPT Academic:学术研究的智能助手
    C++ 快速清空队列
    javaweb基础之浅聊Cookie和Session
    计算点在线上的投影坐标
    JVM详解(InsCode AI 创作助手)
  • 原文地址:https://blog.csdn.net/weixin_46841376/article/details/134179457