[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 ...
随机推荐
- 一次非典型的SQL报错
昨天调试一个表值函数,结果出现了这个错误. mplicit conversion of varchar value to varchar cannot be performed because the ...
- python基础教程第2章——列表与元组笔记
1.序列是Python中最基本的数据结构.序列中的每个元素被分配一个序列号——元素的位置,也称索引,第1个索引是0,第2为1,以此类推.序列中的最后1个元素为-1,倒数第2个位-2. python中有 ...
- MFC中SQLite数据库的使用
1打开数据库 BOOL playDlg::openData() { WCHAR a[100]; CString path; path = m_exePath+L"sentence_makin ...
- Ubuntu下Geary安装
sudo add-apt-repository ppa:yorba/ppasudo apt-get updatesudo apt-get install geary
- JQeury Image LazyLoad
使用jquery插件实现图片延迟加载技术 http://www.cnblogs.com/szytwo/archive/2012/12/27/2836141.html EasyUI http://www ...
- SQL Server创建LinkServer
USE [master] GO /****** Object: LinkedServer [xxx_LNK] Script Date: 2014/7/7 17:04:13 ******/ EXEC m ...
- 安装Logstash
安装Logstash: Logstash 需要 Java 7或者以后版本,使用官方的Oracle发布或者一个开源发布版本比如OpenJDK 检查Java 版本,运行下面的命令: zjtest7-fro ...
- XCode 7上传遇到ERROR ITMS-90535 Unexpected CFBundleExecutable Key. 的解决办法(转)
原文:http://blog.csdn.net/wxs0124/article/details/48706685 找到腾讯的info.plist 删除箭头指向的一行 重新打包,上传. (注明,不一定是 ...
- 安卓图片框架:universal-image-loader的高速使用
在安卓开发过程中难免会遇到下面几个情况: 1.图片异步载入 2.图片缓存 3.图片显示 4.其他--(忘记了) 以上的这些情况,可能要自己去写不少代码去实现这些功能.并且对于一些新手,可能写了半天,发 ...
- 动态改变ComboBox下拉框的宽度
在C++Builder中有时下拉框的内容比较长,标准长度下根本显示不完,可以调用PostMessage()方法来实现 ::PostMessage(comb->Handle,CB_SETDROPP ...