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.

这个移动必须是在原地一动,不能借助其他的容器。

基本的思想是将所有的不是0的数都往前移动,最后在根据容器的最初大小补上0就可以了

 #include <vector>
using namespace std;
class Solution{
public:
void moveZeros(vector<int> & nums){
auto sz = nums.size();
int pos = ;
for (int i = ; i < sz; ++i){
if (nums[i] != )
nums[pos++] = nums[i];
}
for (int i = pos; i < sz; ++i)
nums[i] = ;
}
};

大体上就是这样

java版本代码如下:

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

LeetCode OJ :Move Zeroes (移动0)的更多相关文章

  1. [LeetCode] 283. Move Zeroes ☆(移动0到最后)

    描述 给定一个数组nums,写一个函数,将数组中所有的0挪到数组的末尾,维持其他所有非0元素的相对位置. 举例: nums = [0, 1, 0, 3, 12], 函数运行后结果为[1, 3, 12, ...

  2. 【leetcode】Move Zeroes

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

  3. 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 ...

  4. 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 ...

  5. leetcode 283. Move Zeroes -easy

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

  6. [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 ...

  7. 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 ...

  8. 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 ...

  9. 11. 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 ...

随机推荐

  1. 购物单问题—WPS使用excel

    ****     180.90       88折 ****      10.25       65折 ****      56.14        9折 ****     104.65        ...

  2. 移动端笔记——jQuery touch事件

    判断移动端还是pc端 function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = new Array("A ...

  3. SqlHelper简单实现(通过Expression和反射)5.Lambda表达式解析类

    这个ExpressionHelper类,是整个SqlHelper中,最核心的一个类,主要功能就是将Lambda表达式转换为Sql语句.同时这个转换过程比较复杂,所以下面详细讲解一下思路和这个类的作用. ...

  4. 用python实现0到9之间10个数字排列不重复的个数

      """ product 笛卡尔积 permutations 排列 combinations 组合,没有重复 combinations_with_replacement ...

  5. springboot与redis的无缝整合(分布式)

    参考博客 : https://blog.csdn.net/csao204282/article/details/54092997 1 首先得引入依赖 <dependency> <gr ...

  6. docker 在windows上的使用

    Docker ToolBox 安装 1.首先,安装Docker ToolBox,其中包含了Docker三剑客: docker , docker-machine 和 docker-compose . 安 ...

  7. echo指令

    1.在Linux中echo命令用来在标准输出上显示一段字符,比如:echo "the echo command test!" 这个就会输出“the echo command tes ...

  8. 转换数据库连接池为hikaricp

      hikaricp号称是最快的,今天转换过来试试 1.修改配置文件 <!-- Hikari Datasource --> <bean id="dataSource&quo ...

  9. 配置nginx,Tomcat日志记录请求耗时

    由于公司的业务比较特殊,对速度比较在意,客户最近反应我们的平台时间比较久,处理一个请求十秒左右才返回,领导要求找出原因,我想让nginx日志记录请求处理用了多长时间,后端处理用了多长时间,总共用了多长 ...

  10. 一个线程知识点, 一个MongoDB的知识点

    //WINForm窗体中切换前后台线程执行任务: protected void RunOnUI(Action action) { Invoke(action); } protected void Ru ...