# -*- 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的更多相关文章

  1. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  2. 【LeetCode】36 - Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRu ...

  3. 【一天一道LeetCode】#36. Valid Sudoku

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:https://github.com/Zeecoders/LeetCode 欢迎转载,转载请注明出处 (一)题目 Determi ...

  4. 【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 ...

  5. LeetCode:36. Valid Sudoku(Medium)

    1. 原题链接 https://leetcode.com/problems/valid-sudoku/description/ 2. 题目要求 给定一个 9✖️9 的数独,判断该数独是否合法 数独用字 ...

  6. LeetCode:36. Valid Sudoku,数独是否有效

    LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...

  7. leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

    三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...

  8. [LeetCode] 36. Valid Sudoku 验证数独

    Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...

  9. 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

随机推荐

  1. ,gitignore 中规则不起作用的原因

    .gitignore 文件的用途,该文件只能作用于 Untracked Files,也就是那些从来没有被 Git 记录过的文件(自添加以后,从未 add 及 commit 过的文件). 这样则好理解, ...

  2. Three-Color Flag

    问题陈述: 三色旗的问题最早由E.W.Dijkstra所提出,他所使用的用语为Dutch Nation Flag(Dijkstra为荷兰人),而多数的作者则使用Three-Color Flag来称之. ...

  3. Javascript 之内置对象

    JS提供了11种引用类型:Object.Array.Date.Function.Error.RegExp.Math.Number.String.Boolean.Globle.其中包括了三种基本包装类型 ...

  4. MFC中SQLite数据库的使用

    1打开数据库 BOOL playDlg::openData() { WCHAR a[100]; CString path; path = m_exePath+L"sentence_makin ...

  5. python手记(51)

    python通过声音将文件内容隐藏,实现原理是将文件的内容分别插入到声音文件的不同位置中做为当次采样的数据,目前是对英文文本文档加解密 #!/usr/bin/env python # -*- codi ...

  6. SpringMVC中使用zTree

    1  前端页面 <%@ page language="java" import="java.util.*" pageEncoding="UTF- ...

  7. javascript 汉字拼音排序

    定义和用法 用本地特定的顺序来比较两个字符串. 语法 stringObject.localeCompare(target) 参数 描述 target 要以本地特定的顺序与 stringObject 进 ...

  8. 剖析magento中关于Email模板的设置

    public function send()    {        $emailTemplate = Mage::getModel('core/email_template');        // ...

  9. Eclipse快捷键大全(一)

    Eclipse快捷键大全(一) 常用(系统默认): 1.Format (自动排版) : Ctrl+Shift+F 2.Organize Imports (自动导入) : Ctrl+Shift+O 3. ...

  10. break与continue

    1.break与continue.这两个关键字一般放在循环的花括号里面使用.break——结束整个循环.continue——结束本次循环,进入下次循环. break的案例: int i = 1; fo ...