lintcode刷题-移动零 python
2017-03-23 20:51
495 查看
给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序
注意事项1.必须在原数组上操作2.最小化操作数样例给出 nums =
[0, 1, 0, 3, 12], 调用函数之后, nums = [1, 3, 12, 0, 0].
class Solution:
# @param {int[]} nums an integer array
# @return nothing, do this in-place
def moveZeroes(self, nums):
# Write your code here
i=0
j=0
while i<len(nums):
if nums[i] != 0:
nums[j]=nums[i]
j+=1
i+=1
else:
i+=1
while j<len(nums):
nums[j]=0
j+=1
注意事项1.必须在原数组上操作2.最小化操作数样例给出 nums =
[0, 1, 0, 3, 12], 调用函数之后, nums = [1, 3, 12, 0, 0].
class Solution:
# @param {int[]} nums an integer array
# @return nothing, do this in-place
def moveZeroes(self, nums):
# Write your code here
i=0
j=0
while i<len(nums):
if nums[i] != 0:
nums[j]=nums[i]
j+=1
i+=1
else:
i+=1
while j<len(nums):
nums[j]=0
j+=1
相关文章推荐
- [LintCode 539] 移动零(Python)
- LintCode 移动零 python
- Python写自动化之文件、目录的一些操作(新增、移动、删除)
- 用Python高效地移动文件
- python里将list中元素依次向前移动一位
- python 移动指定目录下的txt文件到另外的目录
- python 文件删除 移动 复制等操作
- Python file.seek()文件游标移动操作方法
- 拥抱Android:编译python搭建移动的无线服务器平台
- Python脚本在Appium库上对移动应用实现自动化测试
- Python 查找指定目录下.txt文件并移动到指定目录
- python clipboard 块移动
- Python:pygame游戏编程之旅二(自由移动的小球)
- python 文件、目录操作(新增、移动、删除等)
- python里将list中元素依次向前移动一位
- python开发_tkinter_图形随鼠标移动
- Python:pygame游戏编程之旅二(自由移动的小球)
- 使用python移动飞信模块发送短信
- Head First Python(移动应用开发)
- 【lintcode】用最高效的方式将数组中的0移动到数组的左边