Leetcode 数组:283 removezeros 移动零(python)
给定一个数组 nums
,编写一个函数将所有 0
移动到数组的末尾,同时保持非零元素的相对顺序。
示例:
输入: [0,1,0,3,12] 输出: [1,3,12,0,0]
说明:
- 必须在原数组上操作,不能拷贝额外的数组。
- 尽量减少操作次数。
class Solution: def moveZeroes(self, nums: List[int]) -> None: """ Do not return anything, modify nums in-place instead. """ for i in nums: if i == 0: nums.append(0) nums.remove(i)
# Cyclic Sortclass Solution: def moveZeroes(self, nums: List[int]) -> None: """ Do not return anything, modify nums in-place instead. """ i = j = 0 for i in range(len(nums)): if nums[i] != 0: nums[j] , nums[i]= nums[i] , nums[j] j += 1 作者:shan-yin-lu-de-xia-tian-2 链接:https://leetcode-cn.com/problems/move-zeroes/solution/python3-bian-li-ti-huan-by-shan-yin-lu-de-xia-tian/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关推荐
diyanpython 2020-11-12
huavhuahua 2020-11-05
Tristahong 2020-10-14
zhaochen00 2020-10-13
LauraRan 2020-09-28
songshijiazuaa 2020-09-01
VitaLemon 2020-09-04
CodeAsWind 2020-08-17
limuxia 2020-08-17
BearRui的AK 2020-08-16
缘起宇轩阁 2020-08-15
范范 2020-07-30
Zaratustra 2020-07-29
归去来兮 2020-07-28
mingyunxiaohai 2020-07-28
willowwgx 2020-07-27
spinachcqb 2020-07-27
mingyunxiaohai 2020-07-19