Datawhale LeetCode腾讯精选50——Task09

LeetCode088 合并两个有序数组

Given two sorted integer arrays nums1 and nums2, 
merge nums2 into nums1 as one sorted array.

The number of elements initialized in nums1 and nums2 are m and n respectively. 
You may assume that nums1 has a size equal to m + n 
such that it has enough space to hold additional elements from nums2.

在这里插入图片描述
思路一:合并后排序
时间复杂度较差,为O((n + m)\log(n + m))O((n+m)log(n+m))。这是由于这种方法没有利用两个数组本身已经有序这一点。
思路二:双指针从前往后,将指针p1 置为 nums1的开头, p2为 nums2的开头,在每一步将最小值放入输出数组中。
由于 nums1 是用于输出的数组,需要将nums1中的前m个元素放在其他地方,也就需要 O(m)O(m) 的空间复杂度。
思路三:双指针从后往前
三种思路均出自LeetCode088 合并两个有序数组官方解决方案

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 instead.
        """
        # two get pointers for nums1 and nums2
        p1 = m - 1
        p2 = n - 1
        # set pointer for nums1
        p = m + n - 1
        
        # while there are still elements to compare
        while p1 >= 0 and p2 >= 0:
            if nums1[p1] < nums2[p2]:
                nums1[p] = nums2[p2]
                p2 -= 1
            else:
                nums1[p] =  nums1[p1]
                p1 -= 1
            p -= 1
        
        # add missing elements from nums2
        nums1[:p2 + 1] = nums2[:p2 + 1]

作者:LeetCode
链接:https://leetcode-cn.com/problems/merge-sorted-array/solution/he-bing-liang-ge-you-xu-shu-zu-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

LeetCode089 格雷编码

The gray code is a binary numeral system 
where two successive values differ in only one bit.

Given an integer n representing the total number of bits in the code, 
return any sequence of gray code.

A gray code sequence must begin with 0.

在这里插入图片描述
在这里插入图片描述
思路一:2位循环添加,对每两个二进制位进行处理
将上一次循环留下的数按位移2位,再依次加上[0,1,3,2]。
思路二:逐位循环添加,将上一次循环留下的数逆序,再按位移1位,最后在二进制最前的一位前再加上1<<(n-2),将所得新数组接到原数组后面。
思路一和二的详细解释和代码参见leetcode 89:格雷编码(python)
思路三:G(i) = i ^ (i / 2),解法参见LeetCode-Python-89. 格雷编码
思路四:递归,解法参见LeetCode 89. 格雷编码 Python 一行代码解法
这里放思路二的代码,出自leetcode 89:格雷编码(python)

class Solution:
    def grayCode(self, n: int) -> List[int]:
        res=[0]
        i=0
        while(i<n):
            rres=res[::-1]
            k=1<<i
            for j in range(len(rres)):
                rres[j]+=k
            res+=rres
            i+=1
        return res

LeetCode089 格雷编码官网精选解决方案

LeetCode104 二叉树的最大深度

Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path 
from the root node down to the farthest leaf node.

在这里插入图片描述
在这里插入图片描述
思路一:层次遍历,变相广度优先搜索,进行一个层次遍历,最后遍历的节点必为深度最大的点。
思路二:递归,递归分别求左右子树的最大深度。
这两个思路和官方提供的解决思路一样,不过官方没有提供python版的代码,所以想用python实现的可以看这篇leetcode 104:二叉树的最大深度(python)
这里放leetcode 104:二叉树的最大深度(python)中思路一的代码。

class Solution:
    def maxDepth(self, root: TreeNode) -> int:
        if root==None:
            return 0
        nodelist=[root]
        deplist=[1,1]
        while nodelist!=[]:
            node=nodelist.pop(0)            
            del deplist[0]
            dep=deplist[0]
            if node.left!=None:
                nodelist.append(node.left)
                deplist.append(dep+1)
            if node.right!=None:
                nodelist.append(node.right)
                deplist.append(dep+1)
        return deplist[-1]

LeetCode104 二叉树的最大深度官方解决方案

任务链接:

team-learning-program/LeetCodeTencent/088 合并两个有序数组.md
team-learning-program/LeetCodeTencent/089 格雷编码.md
team-learning-program/LeetCodeTencent/104 二叉树的最大深度.md

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值