题目来源

https://leetcode.com/problems/valid-sudoku/

etermine 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 '.'. A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.


题意分析
Input: a list means the sudoku with each element in the list is a str

Output:True or False

Conditions:看已有的数字是否满足数独的条件(每行、列、小九方格不能一样的数字)


题目思路

此题是判断已有的矩阵块是否为有效的数独组,分别遍历每一行,每一列,每一个小九方格即可,注意下标范围


AC代码(Python)


 _author_ = "YE"
# -*- coding:utf-8 -*-
class Solution(object):
def verifyRow(self, board):
for i in range(9):
L = []
for j in range(9):
if board[i][j] == '.':
continue
elif board[i][j] in L:
return False
else:
L.append(board[i][j])
return True def verifyColumn(self, board):
for j in range(9):
L = []
for i in range(9):
if board[i][j] == '.':
continue
elif board[i][j] in L:
return False
else:
L.append(board[i][j])
return True def verifySquare(self, board):
for i in range(3):
for j in range(3):
L = []
for k in range(3):
for x in range(3):
if board[3 * i + k][3 * j + x] == '.':
continue
elif board[3 * i + k][3 * j + x] in L:
return False
else:
L.append(board[3 * i + k][3 * j + x])
return True def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
numbers = len(board[0])
print(numbers) # print('row:',self.verifyRow(board))
if not self.verifyRow(board):
return False
# print('column:',self.verifyColumn(board))
if not self.verifyColumn(board):
return False
# print('square:',self.verifySquare(board))
if not self.verifySquare(board):
return False return True #test code
s = Solution()
board = [".87654321","2........","3........","4........","5........","6........","7........","8........","9........"]
print(s.isValidSudoku(board))

[LeetCode]题解(python):036-Valid Sudoku的更多相关文章

  1. [LeetCode] 036. Valid Sudoku (Easy) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...

  2. [Leetcode][Python]36: Valid Sudoku

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 36: Valid Sudokuhttps://oj.leetcode.com ...

  3. 【LeetCode】036. Valid Sudoku

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

  4. LeetCode 036 Valid Sudoku

    题目要求:Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudo ...

  5. leetcode第35题--Valid Sudoku

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

  6. LeetCode(36)Valid Sudoku

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

  7. Java for LeetCode 036 Valid Sudoku

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

  8. 036 Valid Sudoku 有效的数独

    详见:https://leetcode.com/problems/valid-sudoku/description/ class Solution { public: bool isValidSudo ...

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

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

随机推荐

  1. BZOJ3939 : [Usaco2015 Feb]Cow Hopscotch

    设f[i][j]表示到(i,j)的方案数,则有 $f[i][j]=\sum f[x][y](x<i,y<j,a[x][y]!=a[i][j])=\sum f[x][y](x<i,y& ...

  2. MongoDB的安装与CRUD(JAVA)

    http://blogread.cn/it/article/4348?f=wb (MongoDB) 安装: 1)下载MongoDB数据库(这里使用Windows 32 bit版) http://dow ...

  3. tableviewCell折叠状态2

    // //  LHQContentViewCell.h //  11 - 投资管理 - 李洪强 // //  Created by vic fan on 16/4/12. //  Copyright ...

  4. openstack-L版安装

    参照官方install document: http://docs.openstack.org/liberty/install-guide-rdo/ 实验环境:centos7.2 桥接: 192.16 ...

  5. ArcEngine 异常 :The index passed was not within the valid range.

    pRowBuffer.set_Value(pFds.FindField("W_Mean"), Re_mean[3]); 此句代码弹出异常:The index passed was ...

  6. 星外虚拟主机跨web目录文件读取漏洞

    星外虚拟主机跨目录读取文件漏洞,需要一定条件. 问题发生在以下文件,这些文件都没有严格的设置执行权限,当前的IIS用户能够顺利的利用它们执行命令: c:\windows\7i24IISLOG.exe ...

  7. [IT学习]PowerBi 入门

    从哪里开始呢?注册一个账号,从PowerBi的help开始就行了.Get Started会带领你从get data讲起,建立dataset,建立report,一直到dashboard创建. 下面这个链 ...

  8. 自定义view实现侧滑菜单

    自定义View public class SlidingMenu extends HorizontalScrollView { private int mScreenWidth; private in ...

  9. PHP 随机显示

    <?php  print_r(    array_rand(      array(        "新春快乐"=>"",        " ...

  10. PHP函数补完 - var_export

    var_export() 函数返回关于传递给该函数的变量的结构信息,它和 var_dump() 类似,不同的是其返回的表示是合法的 PHP 代码.var_export必须返回合法的php代码, 也就是 ...