leetcode: 36. Valid Sudoku
来源:互联网 发布:王侯将相宁有种乎出自 编辑:程序博客网 时间:2024/06/06 07:47
Q
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
AC
class Solution(object): def isValidSudoku(self, board): """ :type board: List[List[str]] :rtype: bool """ for i in xrange(9): if not self.isValidList([board[i][j] for j in xrange(9)]) or not self.isValidList([board[j][i] for j in xrange(9)]): return False for m in xrange(3): for n in xrange(3): if not self.isValidList([board[i][j] for i in xrange(3 * m, 3 * m + 3) for j in xrange(3 * n, 3 * n + 3)]): return False return True def isValidList(self, xs): xs = filter(lambda x: x != '.', xs) return len(set(xs)) == len(xs)class Solution2(object): def isValidSudoku(self, board): """ :type board: List[List[str]] :rtype: bool """ map_row = [{} for _ in xrange(9)] map_col = [{} for _ in xrange(9)] map_cell = [[{} for _ in xrange(3)] for __ in xrange(3)] for i in xrange(9): for j in xrange(9): char = board[i][j] if char == '.': continue if char in map_row[i]: return False else: map_row[i][char] = [i, j] if char in map_col[j]: return False else: map_col[j][char] = [i, j] if char in map_cell[i / 3][j / 3]: return False else: map_cell[i / 3][j / 3][char] = [i, j] return Trueif __name__ == "__main__": board = [[1, '.', '.', '.', '.', '.', '.', '.', '.'], ['.', 2, '.', '.', '.', '.', '.', '.', '.'], ['.', '.', 3, '.', '.', '.', '.', '.', '.'], ['.', '.', '.', 4, '.', '.', '.', '.', '.'], ['.', '.', '.', '.', 5, '.', '.', '.', '.'], ['.', '.', '.', '.', '.', 6, '.', '.', '.'], ['.', '.', '.', '.', '.', '.', 7, '.', '.'], ['.', '.', '.', '.', '.', '.', '.', 8, '.'], ['.', '.', '.', '.', '.', '.', '.', '.', 9]] assert Solution().isValidSudoku(board)
阅读全文
0 0
- [LeetCode]36.Valid Sudoku
- LeetCode --- 36. Valid Sudoku
- LeetCode 36.Valid Sudoku
- [Leetcode] 36. Valid Sudoku
- 【leetcode】36. Valid Sudoku
- [leetcode] 36. Valid Sudoku
- Leetcode 36. Valid Sudoku
- 36. Valid Sudoku LeetCode
- leetcode 36. Valid Sudoku
- LeetCode *** 36. Valid Sudoku
- leetcode 36. Valid Sudoku
- [LeetCode]36. Valid Sudoku
- LeetCode 36. Valid Sudoku
- leetcode-36. Valid Sudoku
- 36.[Leetcode]Valid Sudoku
- Leetcode 36.Valid Sudoku
- LeetCode-36.Valid Sudoku
- leetcode 36. Valid Sudoku
- noip2017
- 百钱百鸡C语言
- Android 静默安装和静默卸载(系统层)
- 一种基于喊话模式的排序算法
- 有个弹窗模板不错, 就是不知道 怎么弹到的
- leetcode: 36. Valid Sudoku
- Go(全栈)开发企业级后台管理系统
- uva-10299-Relatives-(欧拉函数)
- c++字符串操作函数的源码实现
- 系统之间的通讯方式之(Java AIO异步详解)(五)
- ROS+SLAM笔记(5)创建包
- 2017-11-9
- 通过FTP软件树莓派(Raspberry Pi 3)可以和电脑上传或者下载文件
- 腾讯视频格式转换(重点是CMD的命令)