哈希表题目:有效的数独

news2024/11/25 15:50:41

文章目录

  • 题目
    • 标题和出处
    • 难度
    • 题目描述
      • 要求
      • 示例
      • 数据范围
  • 解法
    • 思路和算法
    • 代码
    • 复杂度分析

题目

标题和出处

标题:有效的数独

出处:36. 有效的数独

难度

2 级

题目描述

要求

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

  1. 每一行必须包含数字 1-9 \texttt{1-9} 1-9 且没有重复。
  2. 每一行必须包含数字 1-9 \texttt{1-9} 1-9 且没有重复。
  3. 每一个九宫格必须包含数字 1-9 \texttt{1-9} 1-9 且没有重复。

注意:

  • 一个有效的数独(部分已被填充)不一定是可解的。
  • 只需要根据以上规则,验证已经填入的数字是否有效即可。

示例

示例 1:

示例 1

输入: board   =   \texttt{board = } board = 
[["5","3",".",".","7",".",".",".","."] \texttt{[["5","3",".",".","7",".",".",".","."]} [["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."] \texttt{,["6",".",".","1","9","5",".",".","."]} ,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."] \texttt{,[".","9","8",".",".",".",".","6","."]} ,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"] \texttt{,["8",".",".",".","6",".",".",".","3"]} ,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"] \texttt{,["4",".",".","8",".","3",".",".","1"]} ,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"] \texttt{,["7",".",".",".","2",".",".",".","6"]} ,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."] \texttt{,[".","6",".",".",".",".","2","8","."]} ,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"] \texttt{,[".",".",".","4","1","9",".",".","5"]} ,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]] \texttt{,[".",".",".",".","8",".",".","7","9"]]} ,[".",".",".",".","8",".",".","7","9"]]
输出: true \texttt{true} true

示例 2:

输入: board   =   \texttt{board = } board = 
[["8","3",".",".","7",".",".",".","."] \texttt{[["8","3",".",".","7",".",".",".","."]} [["8","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."] \texttt{,["6",".",".","1","9","5",".",".","."]} ,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."] \texttt{,[".","9","8",".",".",".",".","6","."]} ,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"] \texttt{,["8",".",".",".","6",".",".",".","3"]} ,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"] \texttt{,["4",".",".","8",".","3",".",".","1"]} ,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"] \texttt{,["7",".",".",".","2",".",".",".","6"]} ,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."] \texttt{,[".","6",".",".",".",".","2","8","."]} ,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"] \texttt{,[".",".",".","4","1","9",".",".","5"]} ,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]] \texttt{,[".",".",".",".","8",".",".","7","9"]]} ,[".",".",".",".","8",".",".","7","9"]]
输出: false \texttt{false} false
解释:除了第一行的第一个数字从 5 \texttt{5} 5 改为 8 \texttt{8} 8 以外,空格内其他数字均与示例 1 相同。但由于位于左上角的九宫格内有两个 8 \texttt{8} 8 存在,因此这个数独是无效的。

数据范围

  • board.length = 9 \texttt{board.length} = \texttt{9} board.length=9
  • board[i].length = 9 \texttt{board[i].length} = \texttt{9} board[i].length=9
  • board[i][j] \texttt{board[i][j]} board[i][j] 是一位数字 1-9 \texttt{1-9} 1-9 或者 ‘.’ \texttt{`.'} ‘.’

解法

思路和算法

这道题要求判断一个已经填入部分数字的数独是否有效,不需要考虑数独是否有解,只需要考虑每一行、每一列和每一个九宫格中的数字是否都是唯一的。以下将一行、一列和一个九宫格统称为「判断单位」。

为了判断数独是否有效,需要记录每一个判断单位中的每个数字的出现情况。数独中的每一个单元格都对应一行、一列和一个九宫格,遍历数组一次即可判断数独是否有效。

对于数独的第 i i i 行第 j j j 列的单元格,其中 0 ≤ i , j < 9 0 \le i, j < 9 0i,j<9,其所在的行下标和列下标分别为 i i i j j j,其所在的九宫格的行编号和列编号分别为 ⌊ i 3 ⌋ \Big\lfloor \dfrac{i}{3} \Big\rfloor 3i ⌊ j 3 ⌋ \Big\lfloor \dfrac{j}{3} \Big\rfloor 3j,由于行编号和列编号都小于 3 3 3,因此可以将行编号和列编号转换为九宫格的编号: ⌊ i 3 ⌋ × 3 + ⌊ j 3 ⌋ \Big\lfloor \dfrac{i}{3} \Big\rfloor \times 3 + \Big\lfloor \dfrac{j}{3} \Big\rfloor 3i×3+3j

有效的数独要求每一个判断单位中每一个数字都只能出现一次,因此可以使用哈希表记录每个数字的出现次数,如果出现次数大于一次则是无效的数独。其实,并不需要记录每个数字的出现次数,只需要记录每个数字是否出现过即可。记录每个数字是否出现过的做法如下。

对于遍历到的每个单元格,如果尚未填入数字则跳过,如果已经填入数字则执行以下操作:

  1. 判断该单元格所在的三个判断单位中该数字是否已经出现过,如果至少一个判断单位中该数字已经出现过,则和当前遍历到的单元格重复,因此是无效的数独,返回 false \text{false} false

  2. 如果三个判断单位中该数字都没有出现过,则将三个判断单位中该数字的状态都更新为已经出现过。

如果遍历结束之后没有出现同一个判断单位中有重复数字的情况,则是有效的数独,返回 true \text{true} true

实现方面有两点技巧:

  1. 由于数独中的数字范围有限且是连续的正整数,因此可以使用数组代替哈希表;

  2. 对于每个判断单位,可以将代替哈希表的数组长度设为 10 10 10,其目的是将下标范围设为 0 0 0 9 9 9,使得下标可以直接和数字对应,不需要进行下标转换。

代码

class Solution {
    public boolean isValidSudoku(char[][] board) {
        boolean[][] rows = new boolean[9][10];
        boolean[][] columns = new boolean[9][10];
        boolean[][] subboxes = new boolean[9][10];
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                char c = board[i][j];
                if (c == '.') {
                    continue;
                }
                int subboxRowIndex = i / 3, subboxColumnIndex = j / 3;
                int subboxIndex = subboxRowIndex * 3 + subboxColumnIndex;
                int digit = c - '0';
                if (rows[i][digit] || columns[j][digit] || subboxes[subboxIndex][digit]) {
                    return false;
                }
                rows[i][digit] = true;
                columns[j][digit] = true;
                subboxes[subboxIndex][digit] = true;
            }
        }
        return true;
    }
}

复杂度分析

  • 时间复杂度: O ( 1 ) O(1) O(1)。数独的大小固定,有 81 81 81 个单元格,对每个单元格遍历一次。

  • 空间复杂度: O ( 1 ) O(1) O(1)。空间复杂度主要取决于记录每一行、每一列和每一个九宫格中出现的数字的哈希表,由于数独的大小固定,因此哈希表的空间也是固定的。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/42899.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

华为22级专家十年心血终成云原生服务网格进阶实战文档,是真的6

前言 有人调侃我们说&#xff1a; 程序员不如送外卖。送外卖是搬运食物&#xff0c;自己是搬运代码&#xff0c;都不产出新的东西……透支体力&#xff0c;又消耗健康&#xff0c;可替代性极强&#xff0c;30岁之后就要面临被优化的危险……想跳槽&#xff0c;但是更高的平台…

如何翻译语音?分享几个翻译语音的方法

不知道大家在上外教的课时&#xff0c;会不会听不懂老师的讲话内容。一般外教老师的口语都是比较规范的&#xff0c;语速还很快&#xff0c;所以我们听不懂他们的讲话也是正常的&#xff0c;不过我们可以借助一些翻译工具来进行语音实时翻译&#xff0c;这样我们就不用担心听不…

Eolink征文活动---Eolink API文档服务的天才产品

实际上我并不是因为这次活动才知道Eolink&#xff0c;早在几年前&#xff0c;我就成为了Eolink的使用者&#xff0c;所以&#xff0c;这次征文活动我势在必行&#xff01;本篇文章将会围绕我如何利用Eolink去解决项目问题进行展开讨论&#xff0c;大致分为以下内容&#xff1a;…

超融合和虚拟化的区别

超融合基础架构(daoHyper-Converged Infrastructure&#xff0c;或简称“HCI”)也被称为超融合架构&#xff0c;如今超融合逐渐成长为目前企业构建软件定义数据中心&#xff0c;乃至未来实现混合云管理架构中一个重要的基础设施支撑平台。那么超融合和虚拟化的区别在哪儿?最重…

K8s 1.23.6版本部署:kubelet-1.23.6+kube-proxy-1.17.6

文章目录前言一、版本环境信息二、服务部署1.基础环境准备2.docker 服务部署&#xff0c;3.k8s服务部署3.1 安装k8s服务&#xff0c;3.2 修改配置文件3.3 执行初始化4.网络插件安装-calico5.dashboard插件安装卸载服务总结前言 K8s 1.23.6版本部署:kubelet-1.23.6kube-proxy-1…

TCP/IP网络编程:P6->基于UDP的服务器端/客户端

本系列文章为《TCP/IP网络编程----尹圣雨》学习笔记&#xff0c;前面的系列文章链接如下 TCP/IP网络编程&#xff1a;P1-&#xff1e;理解网络编程和套接字 TCP/IP网络编程&#xff1a;P2-&#xff1e;套接字类型与协议设置 TCP/IP网络编程&#xff1a;P3-&#xff1e;地址族与…

【期末大作业】基于HTML+CSS+JavaScript南京大学网页校园教育网站html模板(3页)

&#x1f389;精彩专栏推荐 &#x1f4ad;文末获取联系 ✍️ 作者简介: 一个热爱把逻辑思维转变为代码的技术博主 &#x1f482; 作者主页: 【主页——&#x1f680;获取更多优质源码】 &#x1f393; web前端期末大作业&#xff1a; 【&#x1f4da;毕设项目精品实战案例 (10…

【App自动化测试】(十五)手机浏览器(webview)自动化测试

目录1. 手机浏览器自动化前提1.1 安装chromedriver1.2 安装对chromedriver版本1.3 配置capability1.4 设置chromedriver相关配置1.5 使用浏览器的inspect工具远程调试2. 手机浏览器测试代码python版本1. 手机浏览器自动化前提 五大前提&#xff1a; 安装chromedriver安装对ch…

Map集合的概述和接口的使用

目录 一、Map集合概述 1.Map接口的特点 2.方法 二、Map接口的使用 三、Map集合的实现类 1.HashMap 2.Hashtable 3.Properties 4.TreeMap 一、Map集合概述 1.Map接口的特点 &#xff08;1&#xff09;用于存储任意键值对 &#xff08;2&#xff09;键&#xff1a;无序…

阿里P8整合深入理解Dubbo实战+Kafka+分布式设计核心原理内部手册

一 深入理解Apache Dubbo与实战 近年来&#xff0c;随着业务规模的发展和复杂度的增加&#xff0c;传统的单体应用已经很难适应业务迭代的诉求&#xff0c;越来越多的公司开始进行服务化的改造。很高兴看到ApacheDubbo被许多公司采用&#xff0c;作为服务化改造的基础架构进行…

学习常用算法——python

常用算法 时间复杂度 在日常生活中, 我们描述物体的重量使用的是kg, 描述物体的长度使用的是m, 那么相对的, 在计算机科学中也需要一种度量来定性地描述算法的运行时间, 这种度量方法称为大O表示法. 声明f(n)作为我们的函数, n表示的参数. 不同的参数会导致算法运行的时间不同…

超低延时4K级可定制化专业视觉计算平台

> 内置超低延时4K30 ISP IP&#xff0c;ISP延时 0.7 ms > 内置GigE vision IP支持 GigE Vision2.0、GenICam V2.4.0标准&#xff0c;支持用户自定义XML描述文件 > 内置工业机器视觉行业标准的U3 vison IP > 基于FPGA,支持Bayer、YCbCr、RGB等格式,满足高帧率/高…

ElementUI实现在下拉列表里面进行搜索

分析: 首先我们需要实现上图的效果,然后Element-UI的el-select是没有的,所以需要自己写我们需要用到el-popover组件,然后使用它的v-model"visible"来实现控制显示我们在el-popover的slot"reference" 放一个el-select 使用popper-append-to-body"false…

C++ 之 移动构造函数

1、左值和右值 C( 包括 C) 中所有的表达式和变量要么是左值&#xff0c;要么是右值。 通俗的左值的定义就是非临时对象&#xff0c;那些可以在多条语句中使用的对象&#xff0c;表达式结束后依然存在的持久化对象&#xff0c;所有的具名变量或者对象都是左值。右值是指临时的…

<Android开发> Android vold - 第一篇 vold前言简介

本次主要讲解存储模块如U盘等设备在android设备中的管理和使用的模块。本次主要基于android 8.1版本进行解析。不同android版本 vold的内容可能会有所差异。读者可对比阅读解析。 1 Vold介绍 Android中Vold是volume Daemon,即Volume守护进程;Android没有使用Linux平台下的ud…

泛型的介绍和使用方法

目录 一、泛型概述 二、泛型类 三、泛型接口 1.直接在实现类中确定好类型 2.实现类也写成泛型类 四、泛型方法 五、泛型好处 六、泛型集合 1.概念 2.特点 一、泛型概述 1. 本质是参数化类型&#xff0c;把类型作为参数传递。 2. 常见的形式有泛型类、泛型接口、泛型…

【虚幻引擎】UE4/UE5 后期处理盒子(PostProcessVolume)

一、简介 PostProcessVolume&#xff08;后期处理盒子&#xff09;&#xff1a;UE4非常强大的一个后期处理&#xff0c;可以调节画面的色彩&#xff0c;相机的景深&#xff0c;视频的输出效果&#xff0c;环境的光线构造&#xff0c;电影级的氛围感。 二、参数介绍 一、场景中…

驱动开发 Linux按键中断点灯

华清远见上海中心22071班 三个按键实现按键中断&#xff0c; key1->led1 key2->led2 key3->led3 按键按一下灯亮&#xff0c;再按一下灯灭 #include <linux/module.h> #include <linux/init.h> #include <linux/cdev.h> #include <linux/f…

外贸供应链ERP怎么选?全流程综合管理解析

随着外贸体制深入改革、进出口权放开等&#xff0c;以往处于垄断地位&#xff0c;享有种种优惠政策的外贸企业&#xff0c;将面临越来越严峻的国内外市场的竞争及各种挑战。长期以来形成的相对落后的管理体制和经营模式&#xff0c;严重地影响外贸企业在新形势下的生存和发展。…

m基于GA遗传优化+SA模拟退火的混合改进算法的多产品多机器生产优化matlab仿真

目录 1.算法描述 2.仿真效果预览 3.MATLAB核心程序 4.完整MATLAB 1.算法描述 这里&#xff0c;我们首先介绍一下改进算法的基本原理&#xff0c;按照前面说的&#xff0c;这里我们主要将GA和SA进行合并。 这里&#xff0c;我研究了下&#xff0c;将两种算法做如下方法的结合…