[LeetCode]题解(python):036-Valid Sudoku

题目来源:

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



题意分析:

  判断一个数独是不是可以满足规则的,也就是列行和小九宫格都是包括有且仅有1到9.



题目思路:

  刚开始看这个题目的时候觉得就是要先把数独给填好,可以填好就是true的,否者就是false。不过这只是一个easy难度的题目,不可能要这么做吧。然后我测试了一些数据,发现[".87654321","2........","3........","4........","5........","6........","7........","8........","9........"]这个竟然是true。因为如果要玩,那么第一个数填1和9都是错的,所以他应该是不满足的,应该false,但是他的答案是true的。

  所以,题目应该要判断的是当前的状态是不是满足规则的。所以值需要将所有的行列小九宫判断一下就行了。



代码(python):

  

 1 class Solution(object):
 2     def isValidSudoku(self, board):
 3         """
 4         :type board: List[List[str]]
 5         :rtype: bool
 6         """
 7         #列判断
 8         i = 0
 9         while i < 9:
10             j = 0
11             d = {}
12             while j < 9:
13                 if board[i][j] != ‘.‘ and board[i][j] in d:
14                     return False
15                 else:
16                     d[board[i][j]] = True
17                 j += 1
18             i += 1
19         #行判断
20         j = 0
21         while j < 9:
22             i = 0
23             d = {}
24             while i < 9:
25                 if board[i][j] != ‘.‘ and board[i][j] in d:
26                     return False
27                 else:
28                     d[board[i][j]] = True
29                 i += 1
30             j += 1
31         #九宫格
32         i = 0
33         while i < 9:
34             j = 0
35             while j < 9:
36                 m = 0;d = {}
37                 while m < 3:
38                     n = 0
39                     while n < 3:
40                         if board[i + m][j + n] != ‘.‘ and board[i + m][j + n] in d:
41                             return False
42                         else:
43                             d[board[i + m][j + n]] = True
44                         n += 1
45                     m += 1
46                 j += 3
47             i += 3
48         return True



转载请注明出处:http://www.cnblogs.com/chruny/p/4926247.html

时间: 2024-10-09 12:32:21

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

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

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. Valid Sudoku (Easy) 链接: 题目:https://leetcode.com/problems/valid-sudoku/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个数独是否有效. 有效的数独不强求有解. 分析: 只要同一行

[Leetcode][Python]36: Valid Sudoku

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 36: Valid Sudokuhttps://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

【Leetcode】036. 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 '.'. A partially filled sudoku which is valid. Note:A valid Sudoku board (partia

LeetCode 036 Valid Sudoku

题目要求: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 '.'. A partially filled sudoku which is valid. Note:A valid Sudoku

LeetCode(36)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 '.'. A partially filled sudoku which is valid. Note: A valid Sudoku board (partia

leetcode第35题--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 '.'. A partially filled sudoku which is valid. Note:A valid Sudoku board (partial

Java for LeetCode 036 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 '.'. 解题思路: 传说中的数独(九宫格)问题,老实遍历三个规则即可: JAVA实现: static public boolean isValidSudoku(cha

[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

全部最新的题解可以在 我的 github 上找,欢迎 star 和 watch ~ 更新中~~ 说明 这个系列的题解包括用 C++/Java/Python 写的 leetcode 上的算法题目,和 Sql 写的 leetcode 上的数据库题目. 有些题目虽然 AC 了却还没写分析,所以这次就开坑来完成. 链接: 我的 github Leetcode Algorithms Problems Leetcode Database Problems CSDN 题解索引 001.Two_Sum (Med

[LeetCode][JavaScript]Valid Sudoku

https://leetcode.com/problems/valid-sudoku/ 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 '.'. A partially filled sud

[leetcode]Valid Sudoku @ Python

原题地址: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 '.'. A partially filled sudo