Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

Note:

    1. You must do this in-place without making a copy of the array.
    2. Minimize the total number of operations.

inplace的改变一个list,首先要想到的就是可能用two pointers。每次遇到一个非零的nums[j], nums[i]和nums[j]交换,然后 i 指针右移一步

class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
if not nums:
return [] n = len(nums)
ind = 0
for i in range(n):
if nums[i] != 0:
nums[i], nums[ind] = nums[ind], nums[i]
ind += 1

Leetcode Move Zeros的更多相关文章

  1. LeetCode:Move Zeroes

    LeetCode:Move Zeroes [问题再现] Given an array nums, write a function to move all 0's to the end of it w ...

  2. LeetCode 283 Move Zeros

    Problem: Given an array nums, write a function to move all 0's to the end of it while maintaining th ...

  3. leetcode 283 Move Zeros; 27 Remove Elements; 26 Remove Duplicated from Sorted Array;

    ,,,,}; //把数组的值赋给vector vector<int> vec(arr, arr+sizeof(arr)/sizeof(int)); 解法一: 时间复杂度O(n) 空间复杂度 ...

  4. [LeetCode] Move Zeroes 移动零

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  5. [LeetCode283]Move Zeros将一个数组中为0的元素移至数组末尾

    题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...

  6. LeetCode283:Move Zeros

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  7. LeetCode——Move Zeroes

    Description: Given an array nums, write a function to move all 0's to the end of it while maintainin ...

  8. LeetCode Move Zeroes (简单题)

    题意: 给定一个整型数组nums,要求将其中所有的0移动到末尾,并维护所有非0整数的相对位置不变. 思路: 扫一遍,两个指针维护0与非0的交界,将非0的数向前赋值就行了. C++ class Solu ...

  9. leetcode-easy-array-283 move zeros

    mycode  77.24% class Solution(object): def moveZeroes(self, nums): """ :type nums: Li ...

随机推荐

  1. phantomjs模拟登录

    最近在做一些公司其他部门系统的后台模拟操作,但由于那边的系统最开始是外包给其他公司开发的,现在那边的开发也不知道有些post的参数是如何生成的.于是想考察下是不是可以把phantomjs这个工具给加进 ...

  2. 反复请求某个URL缓存严重解决办法

    有2个iframe页面A和B 点击B页面某按钮刷新A,A缓存严重. 后来发现是因为反复请求同样的URL,浏览器就在调用缓存. 解决方法是在URL后添加一个当前时间即可 var url,e=/[?]/g ...

  3. 利用Spring的@Async异步处理改善web应用中耗时操作的用户体验

    Web应用中,有时会遇到一些耗时很长的操作(比如:在后台生成100张报表再呈现,或 从ftp下载若干文件,综合处理后再返回给页面下载),用户在网页上点完按钮后,通常会遇到二个问题:页面超时.看不到处理 ...

  4. JAVA格物致知基础篇:用JAX-RS和Jersey打造RESTful Service

    随着服务器的处理能力越来越强,业务需求量的不断累积,越来越多的公司开始从单一服务器,单一业务承载变成了多服务器,多业务承载的快速扩展的过程中.传统的方法很难满足和应付这种业务量的增长和部署方式的改变. ...

  5. 智普教育Python培训之Python开发视频教程网络爬虫实战项目

    网络爬虫项目实训:看我如何下载韩寒博客文章Python视频 01.mp4 网络爬虫项目实训:看我如何下载韩寒博客文章Python视频 02.mp4 网络爬虫项目实训:看我如何下载韩寒博客文章Pytho ...

  6. "本地泛解析"或者叫做”域名劫持泛解析“,做开发二级域名在内网测试

    都不知道怎么称呼这个好,暂且叫 “本地泛解析” 吧 . 大概就是,要做一个二级域对应一个用户的这种功能,类似博客园,我的博客地址是:jerseyblog.cnblogs.com ,你的博客就可能是 x ...

  7. Toxy新手指南

    Neuzilla出品 官方网站:http://toxy.codeplex.com QQ群:297128022 官方微信公众号: Toxy 是干嘛用的?它是.NET平台上的文件抽取框架,主要解决各种格式 ...

  8. 利用manifest文件对程序目录下的dll进行分类

    1 背景 对于大部分的券商和机构投资者,只能通过有交易所交易系统接入资质的券商提供的柜台系统来进行现货交易.相对于期货市场,现货市场的柜台系统千差万别,接入协议有明文字符串.二进制数据和FIX协议等, ...

  9. SVN 修改log信息报错的解决方案

    要实现允许修改log这个功能,只需要在hooks目录下增加一个名为:pre-revprop-change.bat的文件,重启svn即可.该文件内容为:------------------------- ...

  10. HTTP 错误 500.24 - Internal Server Error的解决方法

    错误提示: 最可能的原因:   system.web/identity@impersonate 设置为 true. 解决办法: 现在经典模式 连微软都几乎放弃了 原设想是为iis不断升级 提供的一种兼 ...