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.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

把一个数组里所有的0都移到后面,不能改变非零数的相对位置关系,而且不能拷贝额外的数组。

要用替换法in-place,双指针来做,前面的指针指向处理过的最后一个非零数字, 后面一个指针向后扫,遇到非零数字,和前面指针所指数字交换位置,前面指针后移1位。

或者不交换只把非零的数字换到前面,最后子统一把后面的全部变成0。

Java: Time Complexity: O(n), Space Complexity: O(1)

public class Solution {
public void moveZeroes(int[] nums) {
int index = 0;
for (int i = 0; i < nums.length; ++i) {
if (nums[i] != 0) {
nums[index++] = nums[i];
}
}
for (int i = index; i < nums.length; ++i) {
nums[i] = 0;
}
}
}

Python:

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

C++:

class Solution {
public:
void moveZeroes(vector<int>& nums) {
int pos = 0;
for (auto& num : nums) {
if (num) {
swap(nums[pos++], num);
}
}
}
};

类似题目:

[LeetCode] 27. Remove Element

All LeetCode Questions List 题目汇总  

[LeetCode] 283. Move Zeroes 移动零的更多相关文章

  1. [leetcode]283. Move Zeroes移零

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

  2. LN : leetcode 283 Move Zeroes

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

  3. LeetCode 283. Move Zeroes (移动零)

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

  4. Leetcode 283. Move Zeroes 移动数组中的零 (数组,模拟)

    题目描述 已知数组nums,写一个函数将nums中的0移动到数组后面,同时保持非零元素的相对位置不变.比如已知nums=[0,1,0,3,12],调用你写的函数后nums应该是[1,3,12,0,0] ...

  5. Java [Leetcode 283]Move Zeroes

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

  6. leetcode 283. Move Zeroes -easy

    题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...

  7. LeetCode 283 Move Zeroes 解题报告

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

  8. LeetCode 283 Move Zeroes(移动全部的零元素)

    翻译 给定一个数字数组.写一个方法将全部的"0"移动到数组尾部.同一时候保持其余非零元素的相对位置不变. 比如,给定nums = [0, 1, 0, 3, 12],在调用你的函数之 ...

  9. Leetcode 283 Move Zeroes python

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

随机推荐

  1. The Shortest Statement(Educational Codeforces Round 51 (Rated for Div.2)+最短路+LCA+最小生成树)

    题目链接 传送门 题面 题意 给你一张有\(n\)个点\(m\)条边的联通图(其中\(m\leq n+20)\),\(q\)次查询,每次询问\(u\)与\(v\)之间的最短路. 思路 由于边数最多只比 ...

  2. 1.zookeeper是干什么的?

    Zookeeper是Hadoop的一个子项目,虽然源自hadoop,但是我发现zookeeper脱离hadoop的范畴开发分布式框架的运用越来越多.今天我想谈谈zookeeper,本文不谈如何使用zo ...

  3. 修改Tomcat启动窗口的名称(Title)

    内容简介 有时在运行项目时,在同一服务器会启动多个Tomcat,很难区分某个tomcat运行的是哪个项目,或者想查看tomcat的端口号,只能去server.xml中查看. 如果能把Tomcat窗口的 ...

  4. 微信小程序——通讯录

    WXML: <view class="flex box box-lr"> <scroll-view class="flex groups box box ...

  5. wget递归下载网站资源

    wget -r -p -np -k http://archive.openwrt.org/barrier_breaker/14.07/ramips/mt7620a/packages/ 在下载https ...

  6. B/S结构与C/S结构测试区别

    B/S结构与C/S结构 B/S结构是浏览器/服务器结构,应用软件的业务逻辑完全在服务器端实现,客户端只需要通过浏览器完成浏览.查询.输入等简单操作. C/S结构是客户端/浏览器结构,客户端具有一定的数 ...

  7. Windbg命令脚本

    命令脚本,就是将完成某个特定任务的相关命令组合在一起,保存在脚本文件里,加载到Windbg里执行,达到我们的目的.你可以理解为脚本就是一种语言,就像c或者汇编,但是他不需要编译器将其编译为可执行文件, ...

  8. graphql-inspector graphql schema比较&&文档校验&&查找破坏性变动工具

    graphql-inspector 是一个方便的graphql 周边工具,可以加速graphql 应该的开发,同时可以帮助我们排查问题 包含以下特性: 进行schema 的比较 文档校验(通过sche ...

  9. Java基础教程--安卓入门教程(七)

    关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 欢迎大家关注我的微信公众号:「醉翁猫咪」 什么是接口? 接口的基本语法 接口的基本语法(一) 使用interface定义 接口当中 ...

  10. Bacteria(优先队列)

    题目链接:http://codeforces.com/gym/101911/problem/C 问题简述:给定n个细胞以及每个细胞的大小,相同的细胞能进行融合,如果能融合到只剩1个细胞则输出需要额外增 ...