LeetCode刷题(7)
来源:互联网 发布:js正则验证身份证号码 编辑:程序博客网 时间:2024/06/08 08:13
判断是否是有效的数独有三点,行不重复,列不重复,每个3x3矩阵不重复
今日事多,参考一个讨论,致谢!
class Solution(object):
def isValidSudoku(self, board):
“””
:type board: List[List[str]]
:rtype: bool
“””
return (self.is_row_valid(board) and
self.is_col_valid(board) and
self.is_square_valid(board))
def is_row_valid(self, board): for row in board: if not self.is_unit_valid(row): return False return Truedef is_col_valid(self, board): for col in zip(*board): if not self.is_unit_valid(col): return False return Truedef is_square_valid(self, board): for i in (0, 3, 6): for j in (0, 3, 6): square = [board[x][y] for x in range(i, i + 3) for y in range(j, j + 3)] if not self.is_unit_valid(square): return False return Truedef is_unit_valid(self, unit): unit = [i for i in unit if i != '.'] return len(set(unit)) == len(unit)
zip()行列互换
set()去重
阅读全文
0 0
- Leetcode刷题(7)
- LeetCode刷题(7)
- 【leetcode】leetcode 刷题 笔记 (不定期更新)
- leetcode刷题(Python)
- LeetCode刷题(1)
- LeetCode刷题(2)
- LeetCode刷题(3)
- LeetCode刷题(废弃)
- LeetCode刷题(FizzBuzz)
- Leetcode刷题(1)
- Leetcode刷题(2)
- Leetcode刷题(3)
- Leetcode刷题(4)
- Leetcode刷题(5)
- Leetcode刷题(6)
- Leetcode刷题(13)
- Leetcode刷题(17)
- LeetCode刷题(18)
- raspberrypi 3代B 配置摄像头
- 内联函数inline
- C#与sql的连接
- 20171011WindowsPrj08_03IAT Hook
- 文章标题
- LeetCode刷题(7)
- POJ
- 1701H2 王建瑜 171011 连续第一天总结
- c++ 建立链表并实现合并
- Oracle笔记九:表的创建及管理
- 148. Sort List
- Ubuntu下 chrome,Java 和tomcat 初次配置
- 基于动态规划的最长公共子序列实现(LCS)
- Android Launcher3分析——开篇