47.leetcode36_valid_suduko
生活随笔
收集整理的這篇文章主要介紹了
47.leetcode36_valid_suduko
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.題目分析
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 (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
2.題目分析
根據note可以知道,這并不是個數獨問題。只需要檢查在每行每列及每個九宮格內數字是否有重復出現的即可
3.解題思路
分別檢查每行每列每個九宮格的數字是否重復。
Python代碼(251ms)
class Solution(object):def isValidSudoku(self, board):""":type board: List[List[str]]:rtype: bool"""temp1=[]temp2=[]for i in range(0,9):temp1.append([])for i in range(0,9):temp2.append([]) #temp1儲存每列數字 for i in range(0,9):for j in range(0,9):temp1[i].append(board[j][i])#temp2儲存每個九宮格數字k=0for i in range(0,9,3):for j in range(0,9,3):for row in range(i,i+3):for col in range(j,j+3):temp2[k].append(board[row][col])k+=1print temp2#開始檢查是否滿足題目要求for i in range(0,9):if self.isValid(board[i])==False or self.isValid(temp1[i])==False or self.isValid(temp2[i])==False:return Falsereturn True#定義isValid函數檢查是否合法def isValid(self,nums):temp=[]for i in range(0,9):if nums[i] not in temp and nums[i]!='.':temp.append(nums[i])continueelse:if nums[i]=='.':continuereturn Falsereturn True?
轉載于:https://www.cnblogs.com/19991201xiao/p/8491006.html
總結
以上是生活随笔為你收集整理的47.leetcode36_valid_suduko的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第二节:Css重写样式
- 下一篇: 使用JavaScript进行表单验证