刷题时遇到了有效数独的问题,该题目也被收录在了leetcode学习库的初级算法这本书里面,今天分享一下个人的解题思路二和代码。
先来看一下题目:


在这里需要注意的是:
1、程序输入为一个二维列表,其中每个数字都是字符串类型的,不是整型
2、列表由’.'和’num’组成
3、该数独不一定是可解的,我觉得这个应该起到了降低难度的作用

思路:看到这个题目的时候首先想到的就是遍历这个数独,既然他要求每行,每列。每个方格都不能出现0-9重复的数字,那么就遍历一遍,发现重复的就返回False就好了。
程序可以分为三个部分进行:
因为不管你按行遍历,还是列遍历还是方格遍历,其最终都是遍历九次,所以外层写一个大循环,然后在内部分别遍历行、列、方块。
1、大循环下遍历行
2、遍历列
3、遍历方块

代码如下:

class Solution:def isValidSudoku(self, board: List[List[str]]) -> bool:for i in range(9):# 行遍历row = []for j in range(9):if board[i][j] != '.':if board[i][j] not in row:row.append(board[i][j])else:return False# 列遍历col = []for j in range(9):if board[j][i] != '.':if board[j][i] not in col:col.append(board[j][i])else:return False# 方格遍历box = []for j in range(9):x = (i//3)*3+j//3y = (i%3)*3+j%3if board[x][y] != '.':if board[x][y] not in box:box.append(board[x][y])else:return Falsereturn True

这是比较常规的解法,我利用的是三个新列表然后判断当前的字符是否在列表内,是的话就返回False,但是要注意每次遍历新的行列方块时,都要保证新列表时空的。
运行效果如下:

个人觉得这道题最难的是获取方块遍历时,每个元素的索引,
这里使用了一个位置来确定每次遍历需要获取的元素位置索引:
x=(i//3)∗3+j//3x=(i//3)*3+j//3 x=(i//3)∗3+j//3
y=(i%3)∗3+j%3y=(i\%3)*3+j\%3 y=(i%3)∗3+j%3
因为想要遍历九个方格,那么就要先获取方格左上角第一个元素的位置:
(0, 0) (0, 3) (0, 6)
(3, 0) (3, 3) (3, 6)

采用遍历方格的顺序时横向遍历,即从左到右横着遍历三个方格,然后从下一组第一个方格开始开始,每个方格内部循环九次用来遍历方格内的九个元素。

Leetcode有效数独的Python解法相关推荐

  1. 【Leetcode】5 longestPalindrome python解法

    leetcode第5题 longestPalindrome 最长回文子串 python解法 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 输入: " ...

  2. LeetCode 15. 3Sum--Java,Python解法

    题目地址: Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? ...

  3. LeetCode刷题之python解法(持续更新)

    1. Two Sum 4行 class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:d = {}fo ...

  4. LeetCode 动态规划(Dynamic programming)系列题目--C++,Python解法

    LeetCode上有许多态规划(Dynamic programming)的题目,我在这里整合一下 本文章不再更新,请看LeetCode 所有题目总结 LeetCode 所有题目总结:LeetCode ...

  5. LeetCode 111. Minimum Depth of Binary Tree--Java, Python解法--二叉树最小高度--迭代,递归

    题目地址:Minimum Depth of Binary Tree - LeetCode Given a binary tree, find its minimum depth. The minimu ...

  6. LeetCode 226. Invert Binary Tree--反转二叉树--C++,Python解法--递归,迭代做法

    题目地址:Invert Binary Tree - LeetCode Invert a binary tree. Example: Input: 4/ \2 7/ \ / \ 1 3 6 9 Outp ...

  7. LeetCode 204. Count Primes--从一开始的质数个数--Python解法--面试算法题

    题目地址:Count Primes - LeetCode Count the number of prime numbers less than a non-negative number, n. E ...

  8. LeetCode 458. Poor Pigs--智力题「小白鼠试毒」--C++,Python解法

    题目地址:Poor Pigs - LeetCode There are 1000 buckets, one and only one of them is poisonous, while the r ...

  9. LeetCode 230. Kth Smallest Element in a BST--C++,Python解法--面试真题--找二叉树中第K小的元素

    题目地址:Kth Smallest Element in a BST - LeetCode Given a binary search tree, write a function kthSmalle ...

  10. LeetCode 221. Maximal Square----动态规划--谷歌面试算法题--Python解法

    题目地址:Maximal Square - LeetCode Given a 2D binary matrix filled with 0's and 1's, find the largest sq ...

最新文章

  1. 一文运维zookeeper
  2. jq常用过滤器_Jquery过滤器
  3. [na]tcpdump参数应用参考
  4. 家用电器用户行为分析与事件识别代码详解+修改后运行无误的代码
  5. SFB 项目经验-07-Skype for Business 话机 Polycom CX700
  6. 数据中台建设五步法(文末赠书)
  7. Ext.ClassManager源码
  8. msys2安装gcc、g++编译器
  9. Linux下面MariaDB 管理命令基础使用
  10. 反射--获取当前子类父类的泛型类型
  11. JS 一个简单的隔行变色函数
  12. vue引入,两种组件库vant,vue-ydui变量冲突问题
  13. 固态和机械硬盘组raid_相同大小的固态硬盘和机械硬盘组RAID1会不会影响固态硬盘的速度?...
  14. 法院裁定抖音违规共享用户信息 多闪被勒令删头像、昵称
  15. 零预算也能用SEO技巧达到Google自然搜寻结果第1名
  16. 数字人民币来了!它与支付宝、微信有什么区别吗?
  17. 【Go基础】1 - Go Go Go
  18. linux下configure命令详细介绍
  19. [Golang]写文件的权限定义perm
  20. prometheus的401、403报错处理

热门文章

  1. 中国新中产家庭“清洁观”:能躺着不站着,能靠科技不靠手
  2. modbus调试团结_团结:打破垃圾收集规则
  3. python做桌面宠物_如何从零开始制作智能桌宠?
  4. 物理专业计算机二级学科,学科门类二级类0702物理学类.doc
  5. python如何调用math函数库函数_Python math函数库
  6. syslinux引导硬盘linux,使用SysLinux引导Linux系统
  7. MATCH和INDEX函数
  8. word pdf 互转
  9. 2015年2月8日工作日志------赵鑫
  10. 从师傅到伙伴:华为背后总是有IBM的影子