zoukankan      html  css  js  c++  java
  • 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=0
            for 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+=1
            print 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 False
            return 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])
                    continue
                else:
                    if nums[i]=='.':
                        continue
                    return False
            return True
  • 相关阅读:
    WeX5那些坑
    项目总结-微信公众平台Html5
    项目总结-APP中的HTML5
    夜幕团队成员的工资究竟几 K ?
    Docker竟然还能这么玩?商业级4G代理搭建实战!
    今天,大佬云集的夜幕团队正式成立了!
    InnoDB物理行中null值的存储的推断与验证
    探究InnoDB数据页内部行的存储方式
    DAO模式
    JDBC
  • 原文地址:https://www.cnblogs.com/19991201xiao/p/8491006.html
Copyright © 2011-2022 走看看