翻译

给定一个数字数组。写一个方法将全部的“0”移动到数组尾部。同一时候保持其余非零元素的相对位置不变。

比如,给定nums = [0, 1, 0, 3, 12],在调用你的函数之后,nums应该变为[1, 3, 12, 0, 0]。

备注:
你必须就地完毕,不得复制该数组。 最小化总共的操作数。
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.

分析

一開始我还以为是要给非零元素排序呢,后来细致一看仅仅是保持相对位置不变就好了。

那就easy非常多了呀。

0 1 0 3 12 (index = 0, current = 0)
1 0 0 3 12 (index = 1, current = 1)
1 0 0 3 12 (index = 1, current = 2)
1 3 0 0 12 (index = 2, current = 3)
1 3 12 0 0 (index = 3, current = 4)

按上面的步骤来,当前的数字是0的话不做操作。非零的话将其与第一个零互换位置。

其核心在于这个第一个零的位置是怎样变化的。即便一開始不是0也没关系,大不了让这个非零数和自己交换位置呗,比方说:

1 2 0 3 12 (index = 0, current = 0)
1 2 0 3 12 (index = 1, current = 1)
1 2 0 3 12 (index = 2, current = 2)
1 2 3 0 12 (index = 3, current = 3)
1 2 3 12 0 (index = 4, current = 4)

翻译成代码就是:

#include <iostream>
#include <vector> using namespace std; void moveZeroes(vector<int>& nums) {
for (int index = 0, current = 0; current < nums.size(); current++) {
if (nums[current] != 0)
swap(nums[index++], nums[current]);
}
} int main() {
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(0);
v.push_back(3);
v.push_back(12); moveZeroes(v); for (auto i : v) {
cout << i << " ";
}
return 0;
}

代码

class Solution {
public:
void moveZeroes(vector<int>& nums) {
for (int index = 0, current = 0; current < nums.size(); current++) {
if (nums[current] != 0)
swap(nums[index++], nums[current]);
}
}
};

LeetCode 283 Move Zeroes(移动全部的零元素)的更多相关文章

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

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

  3. leetcode 283. Move Zeroes -easy

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

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

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

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

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

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

  9. [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. JavaEE-08 JSTL和EL

    学习要点 EL表达式 JSTL标签 EL表达式 为什么需要EL表达式 JavaBean在JSP中的局限 在JSP页面中嵌入大量的Java代码 获取JavaBean属性必须要实例化 强制类型转化 例如, ...

  2. 对数组内容使用了json_encode返回汉字内容返回了空值

    如果使用json_encode对数组进行转成JSON字符串时候,发现汉字的全部为空,这样可以说明的一点是你的页面上用的一定不是UTF8编码,在PHP手册中对json_encode中待编码的值已经说明所 ...

  3. sql语句执行顺序与性能优化(1)

    一.首先我们看一下mysql的sql语句的书写顺序 . select--distinct--from--on--where--group by--having--聚合函数cube.rollup--or ...

  4. MySQL Utilities管理工具

    前提: 1.安装MySQL Utilities工具 2.复制my_print_defaults命令至/usr/bin下或写入环境变量. 卸载方式: python ./setup.py clean -- ...

  5. Mysql ICP(翻译)

    英文版原文链接 https://mariadb.com/kb/en/library/index-condition-pushdown/ ICP 全称 Index Condition Pushdown. ...

  6. tornado框架基础09-cookie和session

    01 cookie 在上节,我们简单了解了登录过程,但是很明显,每次都需要登录,但是在平常逛网站的只需要登录一次,那么网站是如何记录登录信息的呢? 有没有什么办法可以让浏览器记住登录信息,下次再次打开 ...

  7. Django reverse函数

    1.总urls.py内容如下: from django.contrib import admin from django.urls import path from django.conf.urls ...

  8. 剑指Offer(书):重建二叉树

    题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2, ...

  9. Spring Boot集成百度Ueditor

    遇到的问题: 1.将ueditor加入/static目录下,能正常显示,但是出现“请求后台配置项http错误,上传功能将不能正常使用!”(解决在下面,都是自定义上传的,如果需要官方的示例,我也无能为力 ...

  10. VS2013环境下Boost库配置

    序言 最近了解各大互联网公司的校招要求,发现了解Boost程序库也是不可或缺的一部分~ 于是,决定潜心研究下,这个准标准库~ 首先,在官网下载boost的最新版本Boost 1.59.0,这是当前的最 ...