https://leetcode.com/problems/move-zeroes/

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
class Solution:

    # 无法改变参数,必须重新赋值,why?
def moveZeros(self,nums):
n = len(nums)
res = [ x for x in nums if x>0 ] for i in range(n-len(res)):
res.append(0) for i in xrange(len(nums)):
nums[i] = res[i] def moveZeros2(self,nums):
if nums == None or len(nums)==0:
return
j = 0
for i in xrange(len(nums)):
if nums[i]:
nums[i],nums[j] = nums[j],nums[i]
j += 1

Move Zeroes的更多相关文章

  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. [LintCode] Move Zeroes 移动零

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

  3. 【5_283】Move Zeroes

    终于碰到一道水题,睡觉去~ Move Zeroes Total Accepted: 37369 Total Submissions: 88383 Difficulty: Easy Given an a ...

  4. Leetcode-283 Move Zeroes

    #283.   Move Zeroes Given an array nums, write a function to move all 0's to the end of it while mai ...

  5. 283. Move Zeroes(C++)

    283. Move Zeroes Given an array nums, write a function to move all 0's to the end of it while mainta ...

  6. leetcode之旅(7)-Move Zeroes

    Move Zeroes 题目描述: Given an array nums, write a function to move all 0's to the end of it while maint ...

  7. LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List

    283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...

  8. 【leetcode】283. Move Zeroes

    problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...

  9. 【leetcode】Move Zeroes

    Move Zeroes 题目: Given an array nums, write a function to move all 0‘s to the end of it while maintai ...

  10. 283. Move Zeroes【easy】

    283. Move Zeroes[easy] Given an array nums, write a function to move all 0's to the end of it while ...

随机推荐

  1. UIPageControl简单使用

    1.添加一个UIPageControl到view中 -(void)addPageControl { UIPageControl* page=[[UIPageControl alloc]init]; p ...

  2. Java_数组

    一.java数组 1.数组定义:数组就是形象于一个容器(容器即可理解为:装东西的容器) 2.数组特征:数据是连续的,分配大小固定,数组中数据类型完全一致 创建规则:int[] arr = new in ...

  3. snort-2.9.7.0源码安装过程

    2015/02/15,centos6.5-64-minimal,初始205个包 [root@localhost snort]# yum install wget[root@localhost snor ...

  4. js中冒泡事件和捕获事件

    js中冒泡事件和捕获事件: 冒泡事件:冒泡事件是从里向外,即是从被绑定元素开始一直向外到达页面的所有祖先元素都会被触发,这 一过程被称为事件冒泡.这个事件从原始元素开始一直冒泡到DOM树的最上层 捕获 ...

  5. html之ul标签

    html之无序列表,建议使用样式来定义列表的类型. 通常和li配对使用 可选属性: type:disc 圆点,circle圆圈,square方块 compact:显示效果比正常更小巧 <body ...

  6. Android视录视频示例

    这几天需要搞一个Android视频通话功能,从最简单的视频录制开始,网上例子大多不完整.下面的示例参考过别人的代码,还是拿出来给需要的朋友分享下. Activity类:VideoActivity pa ...

  7. js正则及常用方法函数总结

    正则表达式作为一种匹配处理字符串的利器在很多语言中都得到了广泛实现和应用,web开发本质上是处理字符串(服务端接受请求处理后拼接字符串作为响应,这在早期的CGI编程中最明显,然后客户端解析字符串进行渲 ...

  8. javascript 毫秒转日期 日期时间转毫秒

    js毫秒时间转换成日期时间 var oldTime = (new Date("2011/11/11 20:10:10")).getTime(); //得到毫秒数 大多数是用毫秒数除 ...

  9. js net 除法取整

    1.js中 在编程运算中,除法取整数是比较常用的!一般的编程语言都有内置的函数,JS 脚本也不例外.在JavaScript 中,实现除法取整数有两种方法,即是两个内置函数:Math.floor 和Ma ...

  10. mysqlbinlog快速遍历搜索记录 (转)

    目标,开发人员说有个数据莫名其妙添加了,但是不知道是从哪里添加的,而且应用功能里面不应该添加这样的数据,为了查清楚来源,所 以我就准备去binlog里面找了,但是binlog有好几个月的数,我这样一个 ...