1.合并两个有序数组https://leetcode-cn.com/problems/merge-sorted-array/

一开始看到这题在想这个有序数组是升序还是降序是不是需要判断一下

https://www.cnblogs.com/easonbook/p/12876402.html看到了NameError: name 'List' is not defined的解决方法

 1 class Solution:
 2     def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
 3         """
 4         Do not return anything, modify nums1 in-place instead.
 5         """
 6         index_1 = m - 1
 7         index_2 = n - 1
 8         while index_1 >= 0 or index_2 >= 0:
 9             if index_1 != -1 and index_2 != -1:
10                 if nums1[index_1] >= nums2[index_2]:
11                     nums1[index_1 + index_2 + 1] = nums1[index_1]
12                     index_1 -= 1
13                 else:
14                     nums1[index_1 + index_2 + 1] = nums2[index_2]
15                     index_2 -= 1
16             elif index_1 == -1:
17                 nums1[index_2] = nums2[index_2]
18                 index_2 -= 1
19             else:
20                 index_1 -= 1

我的想法和官方解答第三种方法一样(双指针从后往前),比官方效率稍微高一点,时间和空间均排33%

 

 

2.格雷编码https://leetcode-cn.com/problems/gray-code/

 

 

 

3.二叉树的最大深度https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/

posted on 2021-01-20 20:17  zmbreathing  阅读(113)  评论(0编辑  收藏  举报