Leetcode # 283. Move Zeroes

https://leetcode.com/problems/move-zeroes

Solution

Time Complexity: O(len(nums))
Space Complexity: O(1)
(The input and output generally do not count towards the space complexity.)

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

 

Last Updated on 2023/08/16 by A1go

目錄

目錄
Bitnami