面试经典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

boardi.length == 9

boardij is a digit 1-9 or '.'.

题目来源: leetcode

二、题解

我自己的笨方法

cpp 复制代码
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;
    }
};

答案的好方法

cpp 复制代码
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;
    }
};
相关推荐
郝学胜-神的一滴10 分钟前
中级OpenGL教程 022:探秘三维世界的血脉传承——物体父子关系与矩阵递归奥义
c++·线性代数·unity·矩阵·游戏引擎·unreal engine·opengl
拂拉氏22 分钟前
【知识讲解】 链式哈希表的实现与unordered_map和unordered_set的封装
数据结构·哈希算法·散列表
haolin123.26 分钟前
数据结构--二叉树
数据结构
tkevinjd31 分钟前
力扣148-排序链表
算法·leetcode·链表
不如语冰32 分钟前
AI大模型入门-参数的传递
数据结构·人工智能·pytorch·python
ch0sen1pm43 分钟前
刚学完 CAS 原子操作,我花了一晚上写了三个无锁队列
c++
qeen8743 分钟前
【C++】vector的模拟实现详解(二)
c++·学习·算法·迭代器·stl
流浪0011 小时前
数据结构篇(四):线性表——链表——双链表
数据结构·链表
j7~1 小时前
【C++】C++ 继承全解析:从基本语法到菱形继承的底层原理
开发语言·c++·继承·组合·虚继承·#暑假·七月创作之星博客挑战赛
数行拙笔1 小时前
C++客户端---String类型
开发语言·c++·bootstrap