解题思路:按行,列,3*3方格读取元素,存入字典中。若字典中该元素的值大于1,则返回false,否则返回true。

class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
rows = [{} for i in range(9)]
column = [{} for i in range(9)]
box = [{} for i in range(9)]
for i in range(9):
for j in range(9):
# 读取元素
nums = board[i][j]
# 计算3*3方格的下标
index = (i //3) *3 + j//3
if nums!='.':
# 字典的get() 如果nums元素没有,则生成并赋值为0,然后对值加1
rows[i][nums] = rows[i].get(nums,0)+1
column[j][nums] = column[j].get(nums,0)+1
box[index][nums] = box[index].get(nums,0)+1
if rows[i][nums]>1 or column[j][nums]>1 or box[index][nums]>1:
return False
return True

难点:

1. 用字典的值的方法来判断元素是否出现两次

2.3*3方格的下标判断:当i<3时, j//3 决定是第几个方格;当i>3时,(i //3 )*3 + j//3 共同决定是第几个方格。

leetcode第36题:有效的数独的更多相关文章

  1. leetcode第36题--Sudoku Solver

    题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...

  2. LeetCode第[36]题(Java):Valid Sudoku

    题目:有效的数独表 难度:Medium 题目内容: Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be ...

  3. 36、有效的数独 | 算法(leetode,附思维导图 + 全部解法)300题

    零 标题:算法(leetode,附思维导图 + 全部解法)300题之(36)有效的数独 前言 1)码农三少 ,一个致力于 编写极简.但齐全题解(算法) 的博主. 2)文末附赠 价值上百美刀 资料. 一 ...

  4. LeetCode:36. Valid Sudoku,数独是否有效

    LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...

  5. [LeetCode] 36. Valid Sudoku 验证数独

    Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...

  6. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  7. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  8. leetcode第37题--Count and Say

    题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...

  9. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

随机推荐

  1. 无法安装R程序包

    如题,使用insatll.packages("cluster")安装包时,会出现如下错误提示. Warning: unable to access index for reposi ...

  2. Fidder IOS抓包

    Fiddler-HTTPS配置 手机抓包配置 手机网络配置 打开Safari浏览器输入IP+端口号:192.168.0.14:8888,安装证书 证书信任设置:通用 - 关于本机 - 证书信任设置 - ...

  3. 最长特殊序列 II

    最长特殊序列 II class Solution { boolean containsSub(String s,String p){ int i,j; for(i=0,j=0;i<p.lengt ...

  4. tableau 和 R 的连接

    1.安装R包Rserve 2.tableau帮助-管理外部服务连接,单击测试按钮出现成功连接即是通信成功. 3.创建新工作表,设置id字段,针对id记录数创建计算字段Rrand.将Rrand拖入行维度 ...

  5. 覆盖(重写)&隐藏

    成员函数被重载的特征(1)相同的范围(在同一个类中): (2)函数名字相同: (3)参数不同: (4)virtual 关键字可有可无. 覆盖是指派生类函数覆盖基类函数,特征是(1)不同的范围(分别位于 ...

  6. 微信支付的Demo

    是在一个子项目完成的, 依赖: <dependencies> <!-- spring-boot--> <dependency> <groupId>org ...

  7. Mac系统下查看Android studio默认debug签名与正式签名的SHA1值

    https://blog.csdn.net/weixin_32364917/article/details/80095063 获取默认debug签名SHA1值方法,也可以直接打开系统的终端 输入: k ...

  8. 一个理解基本RCNN的简单例子

    对于一个最简单的RNN网络https://github.com/Teaonly/beginlearning/ """Minimal character-level Van ...

  9. Django专题-form表单

    Form介绍 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是否 ...

  10. 用FFmpeg+nginx+rtmp搭建环境实现推流

    Windows: 1.下载文件: 链接:https://pan.baidu.com/s/1c2LmIHHw-dwLOlRN6iTIMg 提取码:g7sj 2.解压文件: 解压到nginx-1.7.11 ...