Leetcode日练笔记14 #1089 #88 Duplicate Zeros & Merge Sorted Array

#1089 Duplicate Zeros

Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right.

Note that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.

解题思路:

for循环找0。找到后把此时idx往后的信息存在temp里。然后在改idx+1后的信息。设置skip= True跳过下一个0。

跳过0后把跳过指令改为false(初始是False)。

class Solution:
    def duplicateZeros(self, arr: List[int]) -> None:
        """
        Do not return anything, modify arr in-place instead.
        """
        idx = 0
        skip = False
        for i in arr:
            if not skip:
                if not i: # i = 0
                    temp = arr[idx:len(arr)-1]
                    arr[idx+1:len(arr)] = temp
                    skip = True
            else:
                skip = False
            idx += 1

Runtime:

emmm,看一下52ms solution:

可以直接用insert改变所有0后面的信息,然后用pop 去掉最后一个。然后在if block判断是否为零里加idx += 1来跳过下个被复制的零。

重写了一下:

class Solution:
    def duplicateZeros(self, arr: List[int]) -> None:
        """
        Do not return anything, modify arr in-place instead.
        """
        idx = 0
        n = len(arr) - 1
        while idx<n:
            if not arr[idx]: # i = 0
                arr.insert(idx+1, 0)
                arr.pop()
                idx += 1
            idx += 1

runtime:

#88 Merge Sorted Array 

You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.

Merge nums1 and nums2 into a single array sorted in non-decreasing order.

The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.

解题思路:

nums1保留[:m],接上nums2 取代nums1.然后再sorted。

但是却不行🚫。

好奇怪。后来看解答说是,这样time complexity太大,sorted(x)的话是O(xlogx)。(但也不应该是🚫?PyCharm跑的话是没问题能跑出来。回头研究一下为啥)

但是直接改掉nums1[m:] =  nums2[:n],再sorted就行。runtime还有85%。

然后推荐了一种三指针的方法。两个读数指针分别在nums1(非零的)和nums2的尾部,还有一个写数指针在nums1的尾部。等于是从尾往头往nums1塞值。比较两读数指针的数值大小,大的写入。

class Solution:
    def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
        p, p1, p2 = m+n-1, m-1, n-1
        while p2 >= 0:
            if p1 >= 0 and nums1[p1] > nums2[p2]:
                nums1[p] = nums1[p1]
                p1 -= 1
            else:
                nums1[p] = nums2[p2]
                p2 -= 1
            p -= 1

runtime:

20ms的solution也是这种solution。

大家提醒,虽然这题是easy,但是要学会用medium的思路解题。不满足于做出来。尽量多种解题思路。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值