有效的数独Python解法
请你判断一个 9 x 9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。
数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
注意:
一个有效的数独(部分已被填充)不一定是可解的。
只需要根据以上规则,验证已经填入的数字是否有效即可。
空白格用 '.' 表示。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/valid-sudoku
例:
输入: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
解析:
按题目要求分三个部分判断,分别为行,列和块即可。
class Solution(object):def isValidSudoku(self, board):""":type board: List[List[str]]:rtype: bool"""for i in range(0, 9): # 外圈遍历row = [] # 行判断数组col = [] # 列判断数组block = [] # 块判断数组for j in range(0, 9): # 内圈遍历if board[i][j] == '.' or board[i][j] not in row: # 行判断row.append(board[i][j])else:return Falseif board[j][i] == '.' or board[j][i] not in col: # 列判断col.append(board[j][i])else:return Falseblock_i = 3 * (i / 3) + j / 3 # 总行 = 大行加小行block_j = 3 * (i % 3) + j % 3 # 总列 = 大列加小列if board[block_i][block_j] == '.' or board[block_i][block_j] not in block: # 块判断block.append(board[block_i][block_j])else:return Falsereturn True
有效的数独Python解法相关推荐
- 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 ...
- 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 ...
- LeetCode 204. Count Primes--从一开始的质数个数--Python解法--面试算法题
题目地址:Count Primes - LeetCode Count the number of prime numbers less than a non-negative number, n. E ...
- LeetCode 458. Poor Pigs--智力题「小白鼠试毒」--C++,Python解法
题目地址:Poor Pigs - LeetCode There are 1000 buckets, one and only one of them is poisonous, while the r ...
- 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 ...
- LeetCode 221. Maximal Square----动态规划--谷歌面试算法题--Python解法
题目地址:Maximal Square - LeetCode Given a 2D binary matrix filled with 0's and 1's, find the largest sq ...
- LeetCode 148. Sort List--面试算法题--C++,Python解法
LeetCode 148. Sort List–面试算法题–C++,Python解法 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大 ...
- LeetCode 20. Valid Parentheses--笔试题--Python解法
题目地址:Valid Parentheses - LeetCode Given a string containing just the characters '(', ')', '{', '}', ...
- LeetCode 145. Binary Tree Postorder Traversal--后序遍历--先序遍历反向输出--递归,迭代--C++,Python解法
题目地址:Binary Tree Postorder Traversal - LeetCode Given a binary tree, return the postorder traversal ...
最新文章
- ExecutorService线程池
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET(转)
- 计算机系统的发展史、基本组成、工作原理
- 简单Unity时间架构设计(克洛诺斯之匙)
- WCF Security userName/Password
- pycharm 离线安装插件
- ZedGraph使用经验
- 卡巴斯基误杀奇虎360
- 英语词汇辨异 —— 形近字、近义词
- 计算机添加路由表命令,win10系统使用dos命令添加静态路由的处理教程
- 如何申请163邮箱账号
- 数据库系统概论-数据库系统阶段的特点
- 实验一 路由器的基本管理
- 检测笔记本电脑的主板,cpu,硬盘的温度
- 计算机数据存储原理 以及 进制
- 计算机与应用数学专业就业方向,2020数学与应用数学专业就业前景和就业方向分析...
- 项目进度管理服务器,工程项目进度管理软件Asta Powerproject——成功项目背后的力量...
- 抓包工具以及如何看抓包信息
- 如何安装SwitchyOmega
- 了解和深入行业/APP分类
热门文章
- Express app.get 进行路由 Route 设置
- SAP S/4HANA OData Mock Service 介绍
- SAP Spartacus public API的概念 - index.ts
- SAP Spartacus RouteGuard路由守卫之CmsPageGuard
- Angular单元测试如何只执行指定的测试用例,提高测试速度
- 一些能提高ABAP开发人员日常工作效率的ABAP小工具
- SAP C4C产品主数据OData服务的ETag处理
- SAP CRM 订单抬头文本的可编辑性讨论
- Method 'GET_ENTITYSET' not implemented in data provider class - correct case
- How myTask application is loaded in CreateFromAccount scenario