翻译

给定一个数字数组。写一个方法将全部的“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. softmax_regression完成mnist手写体数据集的识别

    ---恢复内容开始--- import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnis ...

  2. log4j.xml 精选的log4j.xml文档,比较详细,网上的版本很多,这个版本相对而言比较完整

    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE log4j:configuration PUB ...

  3. js中数组删除 splice和delete的区别,以及delete的使用

    var test=[];test[1]={name:'1',age:1};test[2]={name:'2',age:2};test[4]={name:'3',age:3}; console.log( ...

  4. 《3+1团队》【Alpha】Scrum meeting 3

    项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 3+1团队 团队博客地址 https://home.cnblogs.com/u/3-1group ...

  5. java基本框架

    public class 文件名 { public static void mian(string[] args) { 自己的代码: } }

  6. Swift中Singleton的实现

    一.意图 保证一个类公有一个实例,并提供一个访问它的全局访问点. 二.使用场景 1.使用场景 当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时 当这个唯一实例应该是通过子类化可扩展的,并且 ...

  7. ubuntu中执行docker info出现警告信息WARNING: No memory limit support 或 WARNING: No swap limit support

    docker info 指令报若下错误:WARNING: No memory limit support 或WARNING: No swap limit support 解决方法: 1.打开/etc/ ...

  8. (2) LVS负载均衡:VS_TUN和VS_DR的arp问题

    1. ARP协议简介 ARP(Address Resolution Protocol)协议称为地址解析协议,用于将主机IP地址解析为主机的MAC地址,即IP-->MAC之间一一映射. RARP协 ...

  9. 剑指Offer(书):旋转数组的最小数字

    题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转, ...

  10. LeetCode(12)Integer to Roman

    题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...