码迷,mamicode.com
首页 > 编程语言 > 详细

Leetcode.283 | Move Zeroes(Python)

时间:2020-07-28 14:38:35      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:input   self   copy   number   strong   while   插入   elements   put   

Leetcode.283 Move Zeroes

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.

Example:

Input: [0,1,0,3,12]
Output: [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.

Solution

移位置零

  • 填充
  • 移位
  • 置零
class Solution:
    def moveZeroes(self, nums: list[int]) -> None:
        """
        填充,移位,置零
        """
        # j指向非0数字将要插入的位置
        j = 0
        n = len(nums)
        for i in range(n):
            if nums[i] != 0:
                nums[j] = nums[i]
                j += 1

        for i in range(j, n):
            nums[i] = 0

快慢指针

利用一个指针指向列表第一个0,寻找之后非零数字与其交换

class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        if not nums:
            return nums
        
        zero = 0
        n = len(nums)
        # zero point first 0
        while zero<n and nums[zero]!=0:
            zero+=1
        
        i = zero +1
        while i<n:
            if nums[i] != 0:
                nums[zero],nums[i] = nums[i],nums[zero]
                zero+=1
            i+=1

Leetcode.283 | Move Zeroes(Python)

标签:input   self   copy   number   strong   while   插入   elements   put   

原文地址:https://www.cnblogs.com/xm08030623/p/13390349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!