[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 ...
随机推荐
- nginx解决502错误
添加一个域名绑定,突然服务器就挂了.由于是接受项目,没看过服务器配置.结果nginx和mysql 都跑不起来. 平滑启动nginx失败. 检查/usr/local/nginx/sbin/nginx - ...
- centos6.7配置git服务器
1.yum install -y git 2.adduser git 3.cd /data/git 没有则创建该目录 git init --bare test.git;创建一个裸仓库,没有工作区,不需 ...
- python 初学笔记 (一)
初学python第一天,希望自己真正了解计算机语言,并且做出成效. 写下学习笔记,记录学习进度,娱乐学习,不断成长. python详细介绍: python是什么?运用到哪里?有哪些在使用它? pyth ...
- .Net中String和StringBuilder的区别
String对象是不可变的而StringBuilder则不是这样,可以方便的Append进行扩展. 比如:string aa="123456",那么aa就会在内存中占用一块能放下这 ...
- 解决Easyui1.3.3 IE8兼容性问题
事先声明:项目在Firefox和Chrome上完美运行,在MSIE9.MSIE10上基本没问题,但是放在MSIE8上面运行问题就出来了.登录系统后,系统页面跳动,导致系统无法使用:我使用的是Easyu ...
- ar解压deb包
解压文件 ar -x libstdc++6_4.7.2-5_i386.deb tar -zxvf data.tar.gz
- iOS中UITextView键盘回收
iOS开发中,发现UITextView没有像UITextField中textFieldShouldReturn:这样的方法,那么要实现UITextView关闭键盘,就必须使用其他的方法,下面是可以使用 ...
- hdu 5586 Sum(dp+技巧)
Problem Description There )mod10007.After that,the sum of n numbers should be as much as possible.Wh ...
- g++编译cpp文件
gdb调试c程序打不到断点的原因可能是编译c文件的时候没有加-g选项,-g选项是编译加debug信息的,不加是打不到断点的 g++编译cpp文件 g++ -g -c *.cpp 编译 g+ ...
- CentOS6.4下搭建hadoop2.2(64bit)注意事项
注:本文针对64位机器,32bit课直接tar -zxvf hadoop-2.2.0.tar.gz 解压配置即可. Step1:安装jdk(6以上版本) Step2:下载hadoop--->ht ...