Leetcode 36. 有效的数独

news/2024/11/18 2:41:08/

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

  1. 数字 1-9 在每一行只能出现一次。
  2. 数字 1-9 在每一列只能出现一次。
  3. 数字 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 存在, 因此这个数独是无效的。

提示:

  • board.length == 9
  • board[i].length == 9
  • board[i][j] 是一位数字(1-9)或者 '.'

思考:

用数组记录每个数字出现的次数

  • 数字是从1—9,所以范围要设为10,或者设为9,但是 int temptonum=temp-'0'-1,要再减一
  • if(arr1[i][temptonum]>1||arr2[j][temptonum]>1||arr3[i/3][j/3][temptonum]>1)

    {

           return false;

     }

    这一段要放在if(temp!='.')里面,不然会报错!!!!!!

class Solution {public boolean isValidSudoku(char[][] board) {int[][] arr1=new int[9][10];//每行每个数字出现的次数int[][] arr2=new int[9][10];//每列每个数字出现的次数int[][][] arr3=new int [3][3][10];//每一个小九宫格的每一格数字出现的次数//通过int[i/3][j/3][index+1],得到第i行第j列数字所处在的九宫格中index+1数字出现的次数for(int i=0;i<9;i++){for(int j=0;j<9;j++){char temp=board[i][j];if(temp!='.'){int temptonum=temp-'0';//得到int类型的字符,5arr1[i][temptonum]++;arr2[j][temptonum]++;arr3[i/3][j/3][temptonum]++;if(arr1[i][temptonum]>1||arr2[j][temptonum]>1||arr3[i/3][j/3][temptonum]>1){return false;}}}}return true;}
}

 


http://www.ppmy.cn/news/8375.html

相关文章

pycharm-qt5-designer1

pycharm-qt5-designer1一: designer界面介绍1. 新建模板二: 控件箱简介1. Layouts 布局2. Spacers 间隔(透明)3. Button4. Item views5. Item Widgets 条目控件6. Containers 容器7. input Widgets 输入控件8. Display Widgets 显示控件三: 控件属性简介1. sizePolicy: 控件大小…

我用Python做了个动图生成器,把一千个MM生成了GIF设置桌面,只为每天愉悦心情

文章目录序言代码实战序言 现在的年轻人都开始每天保温杯里泡枸杞&#xff0c;这怎么能行呢&#xff1f; 想要每天过的好&#xff0c;美女必然少不了&#xff0c;每天看美女&#xff0c;只为了愉悦心情&#xff0c;心情好了&#xff0c;才长寿。 于是怀揣着愉悦心情的想法&am…

图像简单运算

图像运算是以图像为单位对图像进行数学操作&#xff0c;运算对象以像素点为基本单位&#xff0c;运算结果为一幅灰度分布与原图像不同的新图像。 算术运算与逻辑运算 算术运算与逻辑运算中每次只涉及一个空间像素的位置&#xff0c;所以可以”原地操作“&#xff08;在&#xf…

【博学谷学习记录】大数据课程-学习第一周总结

Linux服务器 对于Linux操作系统来说&#xff0c;其本身是一个整体&#xff0c;包括Linux内核、系统库和系统程序&#xff0c;Linux内核是其最基础的部分&#xff0c;它实现了对硬件资源的管理&#xff0c;并且提供了使用这些硬件资源的通用接口。 自1991年发布Linux内核来&…

【软件质量】软件质量分析总结报告

软件质量-----“软件与明确地和隐含地定义的需求相一致的程度” 一、质量的概念 Ⅰ、传统的质量概念:产品性能是否符合技术规范 Ⅱ、质量是一组固有特性满足要求的程度 Ⅲ、适用性质量 Ⅳ、质量不仅指产品质量&#xff0c;也指过程和体系的质量 二、软件质量 Ⅰ、产品质…

ElasticSearch笔记

ELASTICSEARCH笔记 1、安装elastic search dokcer中安装elastic search &#xff08;1&#xff09;下载ealastic search和kibana docker pull elasticsearch:7.6.2 docker pull kibana:7.6.2&#xff08;2&#xff09;配置 mkdir -p /mydata/elasticsearch/config 创建目…

pytorch搭建yolov3网络

yolov3的整体网络结构 主要包含了两个部分。左边的Darknet-53主干特征提取网络主要用于提取特征。右边是一个FPN金字塔结构。 主干特征提取网络&#xff08;提取特征&#xff09; import math from collections import OrderedDict import torch.nn as nn#------------------…

技术从业者的发展阶段读后感小结

第一&#xff0c;按照阶段可以划分为&#xff1a; 新手-》高级新手-》胜任者-》精通者-》专家 五个层次 第二&#xff0c;每个阶段的具体含义 &#xff08;1&#xff09;新手&#xff0c;该阶段主要是学习有关工作的规则和套路。比如使用什么工具&#xff0c;用什么框架&#x…