题解 | 移动 0
移动 0
https://www.nowcoder.com/practice/102586387caa4afcbad6f96affce9780
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param nums int整型一维数组
# @return int整型一维数组
#
class Solution:
def moveZeroes(self , nums: List[int]) -> List[int]:
# write code here
n, i, step = len(nums), 0, 0#数组长度,当前下标,总操作次数
while step<n:#总操作次数小于n次
if nums[i]==0:#遇到0时弹出加到末尾,下标不变
nums.pop(i)
nums.append(0)
else:#非0时下标加1
i += 1
step += 1#每次操作数加一
return nums

