
Problem
Solution
Analysis
Python implementation
1 2 3 4 5 6 7 8 9 10 11 12 13
|
class : def moveZeroes(self, nums): """ :type nums: List[int] :rtype: void Do not return anything, modify nums in-place instead. """ point = 0 for i in range(len(nums)): if nums[point]: point += 1 elif nums[i]: nums[point], nums[i] = nums[i], 0 point += 1
|
Java implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
class { public void moveZeroes(int[] nums) { int point = 0; for(int i = 0; i < nums.length; i++){ if(nums[point] != 0) point ++; else if(nums[i] != 0){ nums[point] = nums[i]; nums[i] = 0; point ++; } } } }
|
Time complexity
O(N).
Space complexity
O(1).
Link
283. Move Zeroes
(中文版) 算法笔记: 力扣#283 移动零
近期评论