当前位置:网站首页>leetcode 36. 有效的数独(模拟题)
leetcode 36. 有效的数独(模拟题)
2022-08-09 08:18:00 【_刘小雨】
作者简介:C/C++ 、Golang 领域耕耘者,创作者
个人主页:作者主页
活动地址:CSDN21天学习挑战赛
题目来源: leetcode官网
如果感觉博主的文章还不错的话,还请关注 、点赞 、收藏🧡三连支持一下博主哦~~~
题目描述
请你判断一个 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
示例2:
输入: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”]]
输出:false
解释:除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
🧡 算法分析
此题方法是模拟题
直接模拟就行了
代码实现
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
// 模拟题
// 判断行
bool st[9];
for(int i =0; i < 9; i ++)
{
memset(st, 0, sizeof st);
for(int j = 0; j < 9; j ++)
if(board[i][j] != '.')
{
int t = board[i][j] - '1'; // 因为只开了0-8的数组空间,这里映射一下
if(st[t]) return false;
st[t] = true;
}
}
// 判断列
for(int i =0; i < 9; i ++)
{
memset(st, 0, sizeof st);
for(int j = 0; j < 9; j ++)
if(board[j][i] != '.')
{
int t = board[j][i] - '1';
if(st[t]) return false;
st[t] = true;
}
}
// 判断小方格
for(int i = 0; i < 9; i += 3)
for(int j =0; j < 9; j +=3)
{
memset(st, 0, sizeof st);
for(int x = 0; x < 3; x ++)
for(int y = 0; y < 3; y ++)
if(board[x + i][y + j] != '.')
{
int t= board[i + x][j + y] - '1';
if(st[t]) return false;
st[t] = true;
}
}
return true;
}
};
执行结果:
时间复杂度分析
略
如果觉得对你有帮助的话:
点赞,你的认可是我创作的动力!
🧡 收藏,你的青睐是我努力的方向!
️ 评论,你的意见是我进步的财富!
边栏推荐
猜你喜欢

Routing configuration forwarding and experiment

Introduction to the Endpoint

Three handshakes, four waves

NAT地址转换的原理与配置

App测试

VMware virtual machine cannot be connected to the Internet after forced shutdown

正则之re模块

HOOPS是什么?这4款3D软件开发工具包你还不知道?

The MySQL database

Object detection app based on appinventor and EasyDL object detection API
随机推荐
App测试
Process synchronization and mutual exclusion problem
A watch - article HongMeng development practical experience
Redis(八)集群
OpenHarmony轻智能产品开发直播笔记
jdbctemplate connects to sql server, the data found in the code is inconsistent with the database, how to solve it?
OpenHarmony开源见面会(南京站)相关笔记
动态设置img标签图片失效问题
Literature retrieval operation code
Account and Permission Management
I'm here to advertise
jdbctemplate连接sql server,代码中查出来的数据跟数据库中不一致,如何解决?
Non-decreasing Array
基于appinventor与EasyDL物体检测API的物体检测app
三次握手,四次挥手
bs4之爬取诗词学习
mysql事务(详解)
requests之数据解析Xpath介绍
网络层协议介绍
web基本概念