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. gitlab两种连接方式:ssh和http配置介绍

    gitlab环境部署好后,创建project工程,在本地或远程下载gitlab代码,有两种方式:ssh和http (1)ssh方式:这是一种相对安全的方式 这要求将本地的公钥上传到gitlab中,如下 ...

  2. 转载和积累系列 - 深入理解HTTP协议

    深入理解HTTP协议 1. 基础概念篇 1.1 介绍 HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写.它的发展是万维网协会(World Wide Web C ...

  3. Java的super调用案例: super.getClass()返回的是子类自己

    If you override a method from your superclass (or your superclass's superclass etc.), super.theMetho ...

  4. MathType 公式后的空格问题

    注册表编辑器修改 Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Design Science\DSMT6\WordC ...

  5. [转]各种有用的PHP开源库精心收集

    FROM : http://my.oschina.net/caroltc/blog/324024 1.html2ps and html2pdf    下载地址: http://www.tufat.co ...

  6. 离线安装 Cloudera ( CDH 5.x )

    要配置生产环境前,最好严格按照官方文档/说明配置环境.比如,官方说这个安装包用于RETHAT6, CENTOS6,那就要装到6的版本下,不然很容易出现各种各样的错. 配置这个CDH5我入了很多坑,最重 ...

  7. WinObjc - 使用iOS项目生成通用Windows应用

    Github上一周年的WinObjc项目最近发布了预览版本,终于等到了这一天.WinObjc项目就是Build 2015大会上微软宣布的Project IslandWood项目,致力于将iOS应用快速 ...

  8. 学习SQLite之路(一)

    工作快一年了,接触的东西不是很多,学到的东西也不多.无意中看到公司的代码有一点关于sqlite3的(不是我这一层负责的代码),于是乎就学学试试. 参考: http://www.runoob.com/s ...

  9. Code Review 五问五答

    Code Review 是什么? Code Review即代码审查,程序猿相互审核对方的代码. Code Review能获得什么好处? 提高代码可维护性 你写的代码不再只有编译器看了,你得写出审核人能 ...

  10. C语言函数sscanf()的用法

    从文件读取数据是一件很麻烦的事,所幸有sscanf()函数. C语言函数sscanf()的用法 sscanf() - 从一个字符串中读进与指定格式相符的数据. 函数原型: int sscanf( st ...