[Leetcode][Python]36: Valid Sudoku
- # -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com'- 36: Valid Sudoku
https://oj.leetcode.com/problems/valid-sudoku/- 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.- ===Comments by Dabay===
先检查每个3x3的格子。
然后遍历board,当遇到数字时,检查其所在的行和列。这里用d_row和d_col记录检查过的行和列,避免重复检查。
'''- class Solution:
# @param board, a 9x9 2D array
# @return a boolean
def isValidSudoku(self, board):
for i in xrange(0, 9, 3):
for j in xrange(0, 9, 3):
d = {}
for x in xrange(i, i+3):
for y in xrange(j, j+3):
if board[x][y] == '.':
continue
n = board[x][y]
if n in d:
return False
d[n] = True- d_row = {}
d_col = {}
for i in xrange(0, 9):
for j in xrange(0, 9):
if board[i][j] == '.':
continue
num = board[i][j]
if i not in d_row:
d = {}
for x in xrange(0, 9):
if board[i][x] == '.':
continue
n = board[i][x]
if n in d:
return False
d[n] = True
d_row[i] = True
if j not in d_col:
d = {}
for y in xrange(0, 9):
if board[y][j] == '.':
continue
n = board[y][j]
if n in d:
return False
d[n] = True
d_col[j] = True
return True- def main():
sol = Solution()
board = [
"53..7....",
"6..195...",
".98....6.",
"8...6...3",
"4..8.3..1",
"7...2...6",
".6....28.",
"...419..5",
"....8..79"
]
print sol.isValidSudoku(board)- if __name__ == '__main__':
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[Leetcode][Python]36: Valid Sudoku的更多相关文章
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】36 - Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRu ...
- 【一天一道LeetCode】#36. Valid Sudoku
一天一道LeetCode 本系列文章已全部上传至我的github,地址:https://github.com/Zeecoders/LeetCode 欢迎转载,转载请注明出处 (一)题目 Determi ...
- 【leetcode】36. Valid Sudoku(判断能否是合法的数独puzzle)
Share Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated accordi ...
- LeetCode:36. Valid Sudoku(Medium)
1. 原题链接 https://leetcode.com/problems/valid-sudoku/description/ 2. 题目要求 给定一个 9✖️9 的数独,判断该数独是否合法 数独用字 ...
- LeetCode:36. Valid Sudoku,数独是否有效
LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- [LeetCode] 36. Valid Sudoku 验证数独
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...
- 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
随机推荐
- Javascript 运动中Offset的bug——逐行分析代码,让你轻松了解运动的原理
我们先来看看这个bug 是怎么产生的. <style type="text/css"> #div1 { width: 200px; height: 200px; bac ...
- linux第七章《档案与目录管理》重点回顾
- 了解单位em和px的区别
这里引用的是Jorux的“95%的中国网站需要重写CSS”的文章,题目有点吓人,但是确实是现在国内网页制作方面的一些缺陷.我一直也搞不清楚px与em之间的关系和特点,看过以后确实收获很大.平时都是用p ...
- 以前用Delphi写的CSDN免积分下载器
用了AlphaControl皮肤组件,原理很简单,就是CSDN的一个漏洞.我主要是使用WinInet来获取相关信息,然后从里面分析出真实的下载URL,同时我也是用了AES加密.function Get ...
- Codeforces 306B
#include <cstdio> #include <algorithm> #include <cstring> #include <cstdlib> ...
- IIS的Connection由改为close改为Keep-Alive
参考地址:http://jingyan.baidu.com/article/60ccbceb0c884864cab19784.html 官方:http://technet.microsoft.com/ ...
- AndroidUI 侧滑菜单 DrawerLayout的使用
直接上代码: activity_main.xml: <android.support.v4.widget.DrawerLayout xmlns:android="http://sche ...
- linux学习之(三)-文件操作命令
创建一个空文件: touch 文件名 例:touch tom 查看: 查看一个文件的内容命令cat 文件名 例:cat tom 注:cat命令并不能显示文件的所有信息,但屏幕显示的 行数是有 ...
- windows开机启动nginx
1 .http://www.cuplayer.com/player/PlayerCode/Nginx/2014/0919/1577.html 2. http://www.cnblogs.com/xus ...
- vim简单命令教程-firstblood
你想以最快的速度学习人类史上最好的文本编辑器VIM吗?你先得懂得如何在VIM幸存下来,然后一点一点地学习各种戏法. Vim the Six Billion Dollar editor Better, ...