1. 程式人生 > >Leet Code OJ 簡單(四)

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)