1.题目大意

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都移到数组的末端,其它数字顺序不改变。比如给定的是nums = [0, 1, 0, 3, 12],那么输出结果应该是 [1, 3, 12, 0, 0]。要求尽量不要用复制数组的方式来实现,尽量减小操作次数。

2.思路解析

像我这种弱渣看到,第一个想法就是非常基础的做法——把没用的删掉,再在后面的加上0来就好了。

比如这种弱渣做法:

class Solution {
public:
void moveZeroes(vector<int>& nums) {
int n=nums.size();
for(int i=0;i<n;)
{
if(nums[i]==0) {n--;nums.erase(nums.begin()+i);nums.push_back(0);continue;}
i++;
}
}
};

不过runtime看起来比较难看,“Your runtime beats 41.41% of cpp submissions.”。然后我就去讨论区看了看,发现一个特别强的思路:原代码链接

class Solution {
public:
void moveZeroes(vector<int>& nums) {
stable_sort(nums.begin(), nums.end(), [](const int& x, const int& y){return (x && !y);});
}
};

这个思路

93.96% beat rate

stable_sort的第一个参数是起始位置,第二个参数是终止位置,第三个参数则是一个判断。

比如说后面return的如果是x>y,那么这个数组会变成从大到小排序的数组;在这题中,则代表着x是非0数并且y是0的时候就调换顺序,最终0会调整到队尾。

LeetCode - 268. Missing Number - stable_sort应用实例 - ( C++ ) - 解题报告的更多相关文章

  1. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  2. Java [Leetcode 268]Missing Number

    题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...

  3. LeetCode 268. Missing Number (缺失的数字)

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  4. [LeetCode] 268. Missing Number ☆(丢失的数字)

    转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...

  5. [LeetCode] 268. Missing Number 缺失的数字

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  6. 33. leetcode 268. Missing Number

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  7. leetcode 268 Missing Number(异或运算的应用)

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  8. Leetcode 268 Missing Number 位运算

    题意:先将0, 1, 2, ..., n放入数组,然后去掉其中一个值,找到那个值. 这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, ..., n再次放入这个数组, ...

  9. LeetCode 268. Missing Number缺失数字 (C++/Java)

    题目: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mi ...

随机推荐

  1. window下安装composer

    1.什么是composer 一个智能的下载工具.比如说我的项目要安装yii框架,而yii是依赖于其他东西的,仅仅安装yii是不够的,这样会导致我的项目也不能正常运行:怎么办呢,我们可以一个一个手动的将 ...

  2. 高性能mysql:创建高性能的索引

    本文系阅读<高性能MySQL>,Baron Schwartz等著一书中第五章 创建高性能的索引的笔记,索引是存储引擎用于快速找到记录的一种数据结构. 索引对于良好的性能非常关键,尤其是当表 ...

  3. linux配置mysq与navicat关联

    第一步:在linux中安装mysql(执行如下语句) 安装 mysql: yum install mysql yum install mysql-server yum install mysql-de ...

  4. 解决ajax请求(SpringMVC后台)响应415/400/405错误

    解决ajax请求(SpringMVC后台)响应415/400/405错误 后端代码 bean public class user { private String username; private ...

  5. hiveserver2不能启动

    我的hiveserver2一直不能启动,命令行一直卡住不动,然后我就想是不是配置文件没有配置相关的参数,然后就来修改hive-site.xml 最终修改完后的hive-site.xml: <?x ...

  6. SRM32(8)——ADC和DAC

    1.ADC简介 STM32 拥有 1~3 个 ADC(STM32F101/102 系列只有 1 个 ADC)STM32F103至少拥有2个ADC,STM32F103ZE包含3个ADC,这些 ADC 可 ...

  7. php html 静态化 缓存

    <?php // // ob_start(); $cache_name = md5(__FILE__). '.html'; $cache_lifetime = 3600; // echo fil ...

  8. C语言基础——链表的相关操作

    1 #include <stdio.h> #include <malloc.h> #include <string.h> #include <math.h&g ...

  9. C#中如何使用JS脚本

    C#中如何使用JS脚本 目前在做的组态软件中就使用到了js脚本,这部分js脚本是供用户编写的,用户可以通过我们提供的脚本以及js自身的逻辑,用户就可以随心所欲的控制设备的运行.有比较了几款在C#中执行 ...

  10. Divisibility题解

    From lyh 学长 2018.5.3 信(liang)心(liang)杯T3 一道略弱的数论题. 题目描述 给定 n个数,问是否能从中选出恰好 k个数,使得这些数两两之差可以被 m 整除. 输入输 ...