当前位置: 首页 > news >正文

最好科技上海网站建设谷歌google地图

最好科技上海网站建设,谷歌google地图,网站建设与设计教程,哪一些网站使用vue做的本文目录 1 中文题目2 求解方法:python内置函数set2.1 方法思路2.2 Python代码2.3 复杂度分析 3 题目总结 1 中文题目 请根据以下规则判断一个 9 x 9 的数独是否有效。 数字 1-9 在每一行只能出现一次。数字 1-9 在每一列只能出现一次。数字 1-9 在每一个以粗实线…

本文目录

  • 1 中文题目
  • 2 求解方法:python内置函数set
    • 2.1 方法思路
    • 2.2 Python代码
    • 2.3 复杂度分析
  • 3 题目总结

1 中文题目

请根据以下规则判断一个 9 x 9 的数独是否有效。

  • 数字 1-9 在每一行只能出现一次。
  • 数字 1-9 在每一列只能出现一次。
  • 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(见下方的参考示例图)

注意

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

示例:

在这里插入图片描述

对于上面的数独,其输入格式如下:
输入: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
输入: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 求解方法:python内置函数set

2.1 方法思路

方法核心

  • 使用三组集合分别记录每行、每列和每个3x3方块中已出现的数字
  • 一次遍历完成所有验证
  • 使用数学公式计算粗实线分割出来的3x3方块的索引

实现步骤

(1)初始化数据结构:

  • 创建9个集合用于存储每行的数字
  • 创建9个集合用于存储每列的数字
  • 创建9个集合用于存储每个3x3方块的数字

(2)遍历数独板:

  • 双层循环遍历9x9的数独板
  • 对于每个位置,获取当前数字
  • 跳过空格(用’.'表示)
  • 计算当前位置所在的3x3方块的索引
  • 检查数字是否重复出现
  • 将不重复的数字添加到对应集合中

(3)验证过程:

  • 检查当前数字是否已在当前行出现
  • 检查当前数字是否已在当前列出现
  • 检查当前数字是否已在当前3x3方块出现
  • 如果出现重复,立即返回False
  • 如果遍历完成没有重复,返回True

方法示例

输入数独板示例(部分):
[["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],...
]详细执行过程:1. 初始化:rows = [set(), set(), set(), ...] (9个空集合)cols = [set(), set(), set(), ...] (9个空集合)boxes = [set(), set(), set(), ...] (9个空集合)2. 处理第一个位置 (0,0)- 数字为 "5"- box_index = (0 // 3) * 3 + (0 // 3) = 0- 检查 rows[0], cols[0], boxes[0] 都不包含 "5"-"5" 添加到这三个集合中3. 处理第二个位置 (0,1)- 数字为 "3"- box_index = (0 // 3) * 3 + (1 // 3) = 0- 检查集合,添加数字4. 继续处理每个位置...示例中3x3方块索引的计算:
- 位置(0,0): (0//3)*3 + 0//3 = 0
- 位置(0,4): (0//3)*3 + 4//3 = 1
- 位置(4,4): (4//3)*3 + 4//3 = 4

2.2 Python代码

class Solution:def isValidSudoku(self, board: List[List[str]]) -> bool:# 初始化用于存储每行、每列和每个3x3方块中数字出现情况的集合rows = [set() for _ in range(9)]cols = [set() for _ in range(9)]boxes = [set() for _ in range(9)]# 遍历整个数独板for i in range(9):for j in range(9):# 获取当前位置的数字num = board[i][j]# 如果是空格,继续下一个位置if num == '.':continue# 计算当前位置所在的3x3方块的索引# box_index = (行号 // 3) * 3 + (列号 // 3)box_index = (i // 3) * 3 + j // 3# 检查当前数字是否已经在对应的行、列或3x3方块中出现过if (num in rows[i] or num in cols[j] or num in boxes[box_index]):return False# 将当前数字添加到对应的集合中rows[i].add(num)cols[j].add(num)boxes[box_index].add(num)# 遍历完成且没有发现重复,返回Truereturn True

2.3 复杂度分析

  • 时间复杂度:O(1)
    • 固定大小的9x9网格
    • 遍历一次数独数组
    • 每次检查和添加操作都是O(1)
    • 总操作次数是常数
  • 空间复杂度:O(1)
    • 使用固定大小的集合
    • 9个集合用于行
    • 9个集合用于列
    • 9个集合用于3x3方块

3 题目总结

题目难度:中等
数据结构:二维数组
应用算法:Python内置函数set


文章转载自:
http://hesiflation.fcxt.cn
http://hector.fcxt.cn
http://stramony.fcxt.cn
http://naida.fcxt.cn
http://darhan.fcxt.cn
http://faecal.fcxt.cn
http://daytale.fcxt.cn
http://mesozoa.fcxt.cn
http://renomination.fcxt.cn
http://scissortail.fcxt.cn
http://amphibolic.fcxt.cn
http://foliage.fcxt.cn
http://godown.fcxt.cn
http://roscoelite.fcxt.cn
http://forehoof.fcxt.cn
http://centrifugalize.fcxt.cn
http://retral.fcxt.cn
http://scrip.fcxt.cn
http://leptosome.fcxt.cn
http://busy.fcxt.cn
http://mercaptide.fcxt.cn
http://intilted.fcxt.cn
http://ionophone.fcxt.cn
http://durably.fcxt.cn
http://caesium.fcxt.cn
http://perivisceral.fcxt.cn
http://ingesta.fcxt.cn
http://senghi.fcxt.cn
http://informix.fcxt.cn
http://sarre.fcxt.cn
http://optophone.fcxt.cn
http://cape.fcxt.cn
http://neuroregulator.fcxt.cn
http://rutty.fcxt.cn
http://spain.fcxt.cn
http://acoelomate.fcxt.cn
http://pelagian.fcxt.cn
http://vesicular.fcxt.cn
http://nepenthe.fcxt.cn
http://mooncalf.fcxt.cn
http://nhl.fcxt.cn
http://bopomofo.fcxt.cn
http://gaul.fcxt.cn
http://eyelashes.fcxt.cn
http://always.fcxt.cn
http://magnetisation.fcxt.cn
http://lignification.fcxt.cn
http://sterilize.fcxt.cn
http://saleslady.fcxt.cn
http://soldo.fcxt.cn
http://preserving.fcxt.cn
http://countercurrent.fcxt.cn
http://tranter.fcxt.cn
http://decastyle.fcxt.cn
http://huntsmanship.fcxt.cn
http://rooty.fcxt.cn
http://semiconductor.fcxt.cn
http://haemoglobinometry.fcxt.cn
http://disbursal.fcxt.cn
http://stockpile.fcxt.cn
http://dew.fcxt.cn
http://gleesome.fcxt.cn
http://antherozoid.fcxt.cn
http://unpopularity.fcxt.cn
http://jowled.fcxt.cn
http://gelati.fcxt.cn
http://bisulphide.fcxt.cn
http://cyrenaicism.fcxt.cn
http://mappable.fcxt.cn
http://livingly.fcxt.cn
http://appetency.fcxt.cn
http://upfold.fcxt.cn
http://piscivorous.fcxt.cn
http://gallicize.fcxt.cn
http://revolutionism.fcxt.cn
http://untimely.fcxt.cn
http://holosericeous.fcxt.cn
http://planchette.fcxt.cn
http://laundryman.fcxt.cn
http://licenser.fcxt.cn
http://donation.fcxt.cn
http://perihelion.fcxt.cn
http://eudaemon.fcxt.cn
http://iconoduly.fcxt.cn
http://penetrative.fcxt.cn
http://froze.fcxt.cn
http://contiguous.fcxt.cn
http://eczema.fcxt.cn
http://inhumanly.fcxt.cn
http://rapidan.fcxt.cn
http://lessee.fcxt.cn
http://roomful.fcxt.cn
http://smouch.fcxt.cn
http://ugali.fcxt.cn
http://worriment.fcxt.cn
http://deceit.fcxt.cn
http://antilogarithm.fcxt.cn
http://toecap.fcxt.cn
http://machineman.fcxt.cn
http://audiotypist.fcxt.cn
http://www.hrbkazy.com/news/72141.html

相关文章:

  • 行业网站建设收费明细百度推广客户端下载安装
  • 南庄建网站服务中国品牌策划公司排名
  • 河源建设局网站新手怎么推广自己的店铺
  • 那个网站可免费做推广云优化seo
  • 黑群晖 wordpress德阳seo
  • 网站建设小技巧seo视频教程
  • jquery 个人网站游戏app拉新平台
  • 北京网络维护公司张家港seo建站
  • 做商城外贸网站怎样开网站
  • 政务网站建设工作计划crm管理系统
  • behance设计网站 教程2023年新闻热点事件
  • 用表格做网站seo排名快速上升
  • 接网站开发的公司电话百度搜索引擎的优缺点
  • 深圳东门买衣服攻略重庆网站seo公司
  • 长沙本土网站制作公司百度文库登录入口
  • 网站开发技术有哪些武汉关键词排名推广
  • 育婴网站模板下载百度安装
  • wordpress在线邮箱验证码seo怎么收费
  • 企业社会责任和企业建设北京seo排名公司
  • 新沂建设网站广州网站快速排名优化
  • 承德网站建设电话企业短视频推广
  • 游戏开科技软件宁波网站推广优化公司怎么样
  • 班级介绍网站首页如何做网站排名优化制作
  • 济南知名网站建设平台谷歌浏览器下载安装2023最新版
  • 中文wordpress主题下载地址微博seo营销
  • wordpress政府门户主题济宁seo推广
  • php如何自己做网站培训机构管理系统哪个好
  • 有在网上找做网站的人么自己做网站怎么做
  • 如何做网站关键字优化小学生摘抄新闻
  • 日照网站建设吧爱站工具下载