【leetcode?python】 36. Valid Sudoku

数独规则如下:相当于一个9*9的矩阵

代码如下:
#特定的九个格内1-9的个数至多为1
#依次检查每行,每列,每个子九宫格是否出现重复元素,如果出现返回false,否则返回true.
class Solution(object):
    def isValidSudoku(self, board):
        """
        :type board: List[List[str]]
        :rtype: bool
        """
       
        for i in range(len(board)):
            validate=[]
            for j in range(len(board[i])):
                if validate.__contains__(board[i][j]) and board[i][j]!=‘.‘:return False
                elif (board[i][j]<‘1‘ or board[i][j]>‘9‘) and board[i][j]!=‘.‘:return False
                else:validate.append(board[i][j])
        #列表转置
        boardT=map(list, zip(*board))
        
        for i in range(len(boardT)):
            validate=[]
            for j in range(len(boardT[i])):
                
                if validate.__contains__(boardT[i][j]) and boardT[i][j]!=‘.‘: return False
                elif (boardT[i][j]<‘1‘ or boardT[i][j]>‘9‘) and boardT[i][j]!=‘.‘:return False
                else:validate.append(boardT[i][j])
        
        i=0
        while i<=6:
            j=0
            while j<=6:
                validate=[]
                for n in range(3):
                    for m in range(3):
                         if validate.__contains__(board[i+n][j+m]) and board[i+n][j+m]!=‘.‘:return False
                         elif (board[i+n][j+m]<‘1‘ or board[i+n][j+m]>‘9‘) and board[i+n][j+m]!=‘.‘:return False
                         else:validate.append(board[i+n][j+m])
                j+=3
            i+=3
        
        return True

时间: 2024-08-02 15:14:46

【leetcode?python】 36. Valid Sudoku的相关文章

【LeetCode】36 - Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRules.aspx) The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. N

【leetcode?python】Sum Of Two Number

#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100:#2. 用and(&)操作得到所有位上的进位carry=0100;#3. 用xor(^)操作找到a和b不同的位,赋值给a,a=0001:#4. 将进位carry左移一位,赋值给b,b=1000:#5. 循环直到进位carry为0,此时得到a=1001,即最后的sum. #!!!!!!关于负数的运算

【leetcode?python】 19. Remove Nth Node From End of List

双指针思想:两个指针相隔n-1,每次两个指针向后一步,当后面一个指针没有后继了,前面一个指针的后继就是要删除的节点 # Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         self.val = x#         self.next = None class Solution(object):    def removeNthFromEnd(self,

【leetcode?python】 111. Minimum Depth of Binary Tree

# Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = None class Solution(object):    depthList=[]    def minDepth(self, root):        ""&q

【leetcode?python】 203. Remove Linked List Elements

# Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         self.val = x#         self.next = None class Solution(object):    def removeElements(self, head, val):        """        :type head: ListNode

【leetcode?python】 88. Merge Sorted Array

class Solution(object):    def merge(self, nums1, m, nums2, n):        """        :type nums1: List[int]        :type m: int        :type nums2: List[int]        :type n: int        :rtype: void Do not return anything, modify nums1 in-place

【leetcode?python】 58. Length of Last Word

#很简单,利用strip函数去除左右两边的空格,然后用split函数分割成列表 class Solution(object):    def lengthOfLastWord(self, s):        """        :type s: str        :rtype: int        """        if s==None:return 0        slist=s.strip().split(' ')      

【leetcode?python】 225. Implement Stack using Queues

#栈是先进后出 #队列先进先出 class Stack(object):    def __init__(self):        """        initialize your data structure here.        """        self.inQueue=[]        self.outQueue=[] def push(self, x):        """       

【leetcode?python】 Sum of Left Leaves

#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = None class Solution(object):        def dfs(self,root,isLeft):