题目来源

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. python 代码片段8

    #coding=utf-8 # 列表推倒式子 data=[x+1 for x in range(10)] print data even_numbers=[x for x in range(10) i ...

  2. 浅谈MySQL索引背后的数据结构及算法

    摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BT ...

  3. javascript为元素绑定事件响应函数

    javascript中为元素设置响应时间有两种方法. (1):object.onclick=functionName; 这种方法不可以传递参数. (2):object.onclick=function ...

  4. C# 如何判断数据是否为 NaN

    double a = 0 / 0d; if (double.IsNaN(a)){ //do } 在浮点数计算中, 0除以0将得到NaN ,正数除以0将得到PositiveInfinity ,负数除以0 ...

  5. MySQL修改root密码的各种方法整理

    方法一: 1.首先cmd中输入  net stop mysql   //停止Mysql服务 2.然后在my.ini文件中的[mysqld]下面一行添加 skip_grant_tables 3.在cmd ...

  6. Shell 操作练习2

    #! /bin/sh ############################### # -- # # author jackluo # # net.webjoy@gmail.com # ###### ...

  7. Android Studio 想说爱你不容易

    开始使用Android Studio 真是非常痛苦的一段经历,而这一切的根源就在于GFW,俗称“墙” 如果避过墙来安装 AS,其实我已经在另外一篇文章中说明:http://www.cnblogs.co ...

  8. Servlet 编程 http请求类型

    HTTP协议的8种请求类型介绍 HTTP协议中共定义了八种方法或者叫“动作”来表明对Request-URI指定的资源的不同操作方式,具体介绍如下: OPTIONS:返回服务器针对特定资源所支持的HTT ...

  9. 细说LastLogonTimeStamp

    微软在Windows Server 2003中引入了LastLogonTimeStamp属性.管理员们可以利用这个属性查看用户或者计算机最近是否登录过域.根据这些信息,管理员可以对长时间没有登录的账户 ...

  10. Asp.Net:GridView 编辑、删除、自定义分页以后备用

    页面 GridView 绑定:在中,有 <asp:BoundField/>和 <asp:TemplateField><ItemTemplate>嵌套服务器控件 &l ...