zoukankan      html  css  js  c++  java
  • Leet Code OJ 简单(四)

    88.合并两个有序数组     56ms  提交中击败了47.05% 的用户

    class Solution:
        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 instead.
            """
            for i in range(n):
                temp = nums2[i]
                j = m-1
                while j >=0 and nums1[j] > temp:
                    nums1[j+1] = nums1[j]
                    j -= 1
                nums1[j+1] = temp
                m += 1

    100. 相同的树   48 ms 击败了58.92% 的用户

    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        def isSameTree(self, p, q):
            """
            :type p: TreeNode
            :type q: TreeNode
            :rtype: bool
            """
            if not p and not q:
                return True
            if p and q and p.val == q.val:
                left = self.isSameTree(p.left,q.left)
                right = self.isSameTree(p.right,q.right)
                return left and right
            else:
                return False

    101. 对称二叉树   56 ms 击败了81.40% 的用户

    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        def isSymmetric(self, root):
            """
            :type root: TreeNode
            :rtype: bool
            """
            def isSame(left,right):
                if not left:
                    return right == None
                if not right:
                    return left == None
                if left.val == right.val:
                    return isSame(left.left,right.right) and isSame(left.right,right.left)
                else:
                    return False
            if not root:
                return True
            return isSame(root.left, root.right)

    104. 二叉树的最大深度 : 64 ms 击败了79.70% 的用户

    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        def maxDepth(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """         
            if not root:
                return 0
            left =  self.maxDepth(root.left)
            right = self.maxDepth(root.right)
            return 1 + max(left,right)
  • 相关阅读:
    手动档和自动档
    关于目标:骑行里程破万的感想
    JavaScript基础学习-iterable
    第一个mybatisplus
    MAVEN安装配置
    List和ArrayList的区别
    mysql安装
    Nginx的命令
    Windows Server 2008/2012/2016允许多个用户同时远程桌面
    soapui模拟桩-4 将模拟桩打包成war包
  • 原文地址:https://www.cnblogs.com/FanMLei/p/10500980.html
Copyright © 2011-2022 走看看