LeetCode(34)有效的数独【矩阵】【中等】

news/2024/11/7 9:13:25/

在这里插入图片描述

目录

    • 1.题目
    • 2.答案
    • 3.提交结果截图

链接: 36. 有效的数独

1.题目

请你判断一个 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)或者 '.'

2.答案

class Solution {public boolean isValidSudoku(char[][] board) {int[][] block = new int[3][9];int[] line = new int[9];int[] column = new int[9];for (int i = 0; i < 9; i++) {Arrays.fill(line, 0);Arrays.fill(column, 0);if (i % 3 == 0) {for (int j = 0; j < 3; j++) {Arrays.fill(block[j], 0);}}for (int j = 0; j < 9; j++) {// 数字 1-9 在每一行只能出现一次。char lineCar = board[i][j];if ('.' != lineCar) {if (line[lineCar - '0' - 1] != 0) {return false;}line[lineCar - '0' - 1] = 1;}// 数字 1-9 在每一列只能出现一次。char columnChar = board[j][i];if ('.' != columnChar) {if (column[columnChar - '0' - 1] != 0) {return false;}column[columnChar - '0' - 1] = 1;}// 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。if ('.' != lineCar) {if (block[j / 3][lineCar - '0' - 1] != 0) {return false;}block[j / 3][lineCar - '0' - 1] = 1;}}}return true;}
}

3.提交结果截图

在这里插入图片描述

整理完毕,完结撒花~ 🌻


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

相关文章

[操作系统] 大厂必问~虚拟内存系列

文章目录 4.1 什么是虚拟内存(Virtual Memory)?4.2 局部性原理4.3 虚拟存储器4.4 虚拟内存的技术实现4.5 页面置换算法OPT 页面置换算法&#xff08;最佳页面置换算法&#xff09;FIFO&#xff08;First In First Out&#xff09; 页面置换算法&#xff08;先进先出页面置换算…

中国毫米波雷达产业分析4——毫米波雷达企业介绍

一、矽典微 &#xff08;一&#xff09;公司简介 矽典微致力于实现射频技术的智能化&#xff0c;专注于研发高性能无线技术相关芯片&#xff0c;产品广泛适用于毫米波传感器、下一代移动通信、卫星通信等无线领域。 整合自身在芯片、系统、软件、算法等领域的专业能力&#xf…

在PyCharm中配置PyQt5环境

在PyCharm中配置PyQt5环境 文章目录 1.安装第三方库2.PyQt5设计器3.PyUIC转换工具 &#x1f339;꧔ꦿ&#x1f339;꧔ꦿ&#x1f339;꧔ꦿ&#x1f339;꧔ꦿ&#x1f339;꧔ꦿ&#x1f339;꧔ꦿ&#x1f339;꧔ꦿ&#x1f339;꧔ꦿ&#x1f339;꧔ꦿ&#x1f339;꧔ꦿ&#x1…

【论文解读】基于生成式面部先验的真实世界盲脸修复

论文地址&#xff1a;https://arxiv.org/pdf/2101.04061.pdf 代码地址&#xff1a;https://github.com/TencentARC/GFPGAN 图片解释&#xff1a; 与最先进的面部修复方法的比较&#xff1a;HiFaceGAN [67]、DFDNet [44]、Wan 等人。[61] 和 PULSE [52] 在真实世界的低质量图像…

Youtube如何做SEO关键词挖掘

做好Youtube的SEO优化&#xff0c;可以使我们的视频得到更多的展示&#xff0c;更多的点击和观看&#xff0c;就能获得更多的粉丝和流量。一方面通过视频做引流到目标网站进行转化赚钱&#xff0c;另一方面可以通过涨粉接youtube广告赚钱。要做seo最关键的一步在于关键词的挖掘…

消息队列好文收集

Kafka Kafka之ISR机制的理解-CSDN博客

MatchPyramid实现文本匹配

引言 今天利用MatchPyramid实现文本匹配。 原论文解析→点此←。 MatchPyramid 核心思想是计算两段文本间的匹配矩阵&#xff0c;把它当成一个图形利用多层卷积网络提取不同层级的交互模式。 匹配矩阵是通过计算两段输入文本基本单元(比如字或词)之间相似度得到的&#xf…

Android Studio Giraffe版本遇到的问题

背景 上周固态硬盘挂了&#xff0c;恢复数据之后&#xff0c;重新换了新的固态安装了Win11系统&#xff0c;之前安装的是Android Studio 4.x的版本&#xff0c;这次也是趁着新的系统安装新的Android开发工具。 版本如下&#xff1a; 但是打开以前的Android旧项目时&#xff…