LeetcodeArray283 MoveZero +LeetcodeArrary26 Remove Duplicates from Sorted Array

这是我在微软面试过程中,真的遇到的面试题,但我没想到,答案竟然如此简单:

Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

Note:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

class Solution(object):
    def moveZeroes(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        zero = 0  # records the position of "0"
        for i in range(len(nums)):#i是遍历的角标
            if nums[i] != 0:
                nums[i], nums[zero] = nums[zero], nums[i]#我叫这步为step1
                zero += 1#step2
        

思路:比如input=[1,3,0,4,0,9]

当i==0,zero==0时,它们都指向数字1,进行step1,然后->

当i==1,zero==1时,它们都指向数字3,进行step1,然后->

当i==2,zero==2时,它们都指向数字0,不进行step1和2,所以-->也就是说当i指针遇到数字0的时候,zero就不增加了,i就比zero走的快-->

当i==3,zero==2时,i指向数字4,zero指向0,它们进行step1交换位置,数组变为[1,3,4,0,0,9],进行step2-->

当i==4指向数字0时,zero==3指向数字0时,不进行step1,2-->

当i==5指向数字9,时,zero==3时,进行step1交换位置,数组变为[1,3,4,9,0,0],zero+1指向4,程序结束。

总结思路:zero指针永远指向数组里第一个0;i比zero走的一样快或者比zero快,i负责指向需要和zero交换的数,即数组中0后面的第一个非0数。



看到这个答案以后恕我无心看其他更繁琐的答案了。。。

时间复杂度:O(n)

空间复杂度:O(1),因为没有开辟新的存储空间


微软面试官还提示我存储两个游标,根本不用好嘛~让我哭一会


26. Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example:

Given nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the new length.

class Solution(object):
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums: return 0
        
        newTail = 0  # records the position of "new array's tail"
     
        for i in range(1,len(nums)):#i是遍历的角标  
            if nums[i] != nums[newTail]: 
                newTail+=1
                nums[newTail]=nums[i]
               
                
        return  newTail+1

时间复杂度:O(n)

空间复杂度:O(1)

思路:

要求in-place变换数组的题我是需要好好练一练,这道题的思路也没什么高明的,

newTail记录想象中的新数组的最后那个index的脚标。

比如说[0,1,2,2,3,4,4,5,6]这样一个输入按算法跑完之后nums应该是[0,1,2,3,4,5,6,5,6]

并不是真正意义上的移除(可能因为这点题目表述不清楚所以这道题很多差评吧)。

如果遇到重复的元素,newTail并不会移动,而i会移动该元素之后第一个不与该元素重复的数的位置

所以newTail再加1

然后再nums[newTail]=nums[i]

return 的 newTail加1 是因为,最后newTail停住的脚标+1才是真正的没有重复值的数组的长度


总结以上两道题:

movezeros:

zero这个pointer 指向0所在的index

removeduplicatesfromsortedarray:

newTail这个pointer指向想象中新数组的末尾位置


共同点: 都有遍历数组的pointer i

i走的只会比zero/newTail要快

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值