美文网首页
283.移动零

283.移动零

作者: 不死鸟F21 | 来源:发表于2022-01-10 17:46 被阅读0次
1.第一次实现
class Solution:
    def moveZeroes(self, nums) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        num_len = len(nums)
        if not num_len:
            return
        for i in range(num_len-1,-1,-1):
            if nums[i] ==0:
                j = i
                while j+1 < num_len and nums[j+1] !=0:
                    nums[j+1], nums[j] = nums[j],nums[j+1]
                    j +=1

2.两次遍历法

class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n = len(nums)
        j = 0
        for i in range(n):
            if nums[i]:
                nums[j] = nums[i]
                j+=1
        for i in range(j,n):
            nums[i] = 0
  1. 双指针
class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n = len(nums)
        left = right = 0 
        while right < n:
            if nums[right]:
                nums[right],nums[left] = nums[left],nums[right]
                left += 1
            right +=1 

class Solution:
    def moveZeroes(self, nums) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        j = 0 # 记录非0元素的位置
        for i in range(len(nums)):
            if nums[i] !=0:
                nums[j] = nums[i]
                if i!= j:
                    nums[i] =0
                j+=1
        print(nums)

5.如果可以开辟新空间,定义一个新数组,非0往前边放,0往后边放(不满足题目要求,仅仅一个思路)

        nums_new = []
        for i in range(len(nums)):
            if nums[i]:
                nums_new.append(nums[i])
        for i in range(len(nums)):
            if  not nums[i]:
                nums_new.append(nums[i])
        nums = nums_new
        print(nums)

6.不推荐时间,时间复杂度高

1.遍历整个数组
2.遇到0删除
3.列表后边追加0
class Solution:
    def moveZeroes(self, nums) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        for i in range(len(nums)):
            if nums[i] ==0:
                nums.remove(0)  #O(n)
                nums.append(0)

相关文章

  • 283. 移动零

    283. 移动零

  • LeetCode考试

    283. 移动零](https://leetcode-cn.com/problems/move-zeroes/) ...

  • 每日一题20201119(283. 移动零)

    283. 移动零[https://leetcode-cn.com/problems/move-zeroes/] 思...

  • 算法:数组(二)

    283. 移动零 - 力扣(LeetCode) (leetcode-cn.com)[https://leetcod...

  • LeetCode 281-310

    283. 移动零[https://leetcode-cn.com/problems/move-zeroes/] 2...

  • LeetCode:283. 移动零

    问题链接 283. 移动零[https://leetcode-cn.com/problems/move-zeroe...

  • 283. 移动零

    题目地址(283. 移动零) https://leetcode.cn/problems/move-zeroes/[...

  • 283. 移动零

    题目 分析 其实题目本身并不难,主要是他要求必须在原数组上进行操作,不能拷贝额外的数组。这里用到了双指针法,快指针...

  • 283.移动零

    题目 给定一个数组 nums, 编写一个函数将所有 0 移动到它的末尾,同时保持非零元素的相对顺序。 例如, 定义...

  • 283. 移动零

    一、题目原型: 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 二...

网友评论

      本文标题:283.移动零

      本文链接:https://www.haomeiwen.com/subject/shlrcrtx.html