Move Zeroes
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:
- You must do this in-place without making a copy of the array.
- 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的更多相关文章
- LeetCode:Move Zeroes
LeetCode:Move Zeroes [问题再现] Given an array nums, write a function to move all 0's to the end of it w ...
- [LintCode] Move Zeroes 移动零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- 【5_283】Move Zeroes
终于碰到一道水题,睡觉去~ Move Zeroes Total Accepted: 37369 Total Submissions: 88383 Difficulty: Easy Given an a ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【leetcode】283. Move Zeroes
problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...
- 【leetcode】Move Zeroes
Move Zeroes 题目: Given an array nums, write a function to move all 0‘s to the end of it while maintai ...
- 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 ...
随机推荐
- Unity坐标系
Unity 使用的是左手坐标系
- Intel MKL函数,如何得到相同的计算结果?【转】
在运行程序时,我们总希望多次运行的结果,是完全一致,甚至在不同的机器与不同的OS中,程序运行的结果每一位都完全相同. 事实上,程序往往很难保证做到这一点. 为什么呢? 我们先看一个简单的例子: 当程序 ...
- 对OCR文字识别软件的扫描选项怎么设置
说到OCR文字识别软件,越来越多的人选择使用ABBYY FineReader识别和转换文档,然而并不是每个人都知道转换质量取决于源图像的质量和所选的扫描选项,今天就给大家普及一下这方面的知识. ABB ...
- linux包之iproute之ip命令
[root@localhost ~]# rpm -qf /sbin/ipiproute-2.6.32-31.el6.x86_64ip 是个命令, ip 命令的功能很多!基本上它整合了 ifconfig ...
- 【转】php利用mkdir创建多级目录
先介绍一下 mkdir() 这个函数: mkdir($path,0777,true); 第一个参数:必须,代表要创建的多级目录的路径: 第二个参数:设定目录的权限,默认是 0777,意味着最大可能的访 ...
- 【转】PHP 之 CURL 模拟登陆并获取数据
1.CURL模拟登陆的流程和步骤2.tempnam 创建一个临时文件3.使用CURL模拟登陆到PHP100论坛 <?php$cookie_file = tempnam('./temp','coo ...
- jQuery Mobile_页面事件
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- PHP 开发环境配置
使用phpStudy +Zend Studio 使用phpStudy +Zend Studio ,这个组合是我个人使用的比较好的,现在分享出来.一.phpStudy简体中文版 该程序包集成最新的Apa ...
- 面向对象设计模式--观察者模式(Observer)
要点: 1.如何使用观察者模式: 对应使用这个模式的用户(main)来说,subject和observer这两个基类是不被关系的,在调用者(main)中只是有concreteSubject和concr ...
- HackerRank "Manasa and Prime game"
Intuitive one to learn about Grundy basic :) Now every pile becomes a game, so we need to use Spragu ...