编写一个函数此函数接收一个列表 nums要求将nums中所有0移动到列表的末尾同时保持非零元素的相对顺序不变将移动完成之后的列表返回。20分示例 1输入 nums = 010312输出 131200示例2 输入 nums = 07011082输出 71182000
def moveZeroes(nums): count = nums.count(0) # 统计0的个数 nums = [x for x in nums if x != 0] # 去除0 nums.extend([0]*count) # 在末尾添加count个0 return nums
原文地址: http://www.cveoy.top/t/topic/hCXH 著作权归作者所有。请勿转载和采集!