LeetCode - 268. Missing Number - stable_sort应用实例 - ( C++ ) - 解题报告
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:
- You must do this in-place without making a copy of the array.
- 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++ ) - 解题报告的更多相关文章
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- Java [Leetcode 268]Missing Number
题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
- LeetCode 268. Missing Number (缺失的数字)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] 268. Missing Number ☆(丢失的数字)
转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...
- [LeetCode] 268. Missing Number 缺失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 33. leetcode 268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- leetcode 268 Missing Number(异或运算的应用)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- Leetcode 268 Missing Number 位运算
题意:先将0, 1, 2, ..., n放入数组,然后去掉其中一个值,找到那个值. 这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, ..., n再次放入这个数组, ...
- 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 ...
随机推荐
- Servlet过滤器Filter和监听器
一.Servlet过滤器的概念: *********************************************************************************** ...
- iOS视频倒放
iOS视频倒放 视频的倒放就是视频从后往前播放,这个只适应于视频图像,对声音来说倒放只是噪音,没什么意义,所以倒放的时候声音都是去除的. 倒放实现 一般对H264编码的视频进行解码,都是从头至尾进行的 ...
- 在mac下运行 npm run eject 出现报错问题解决方法
当使用create-react-app创建项目后,接着运行npm run eject时,如果出现下面的错误 可能是脚手架添加了.gitignore这个文件,但是没有本地仓库,可以使用以下代码解决这个问 ...
- WebGL学习笔记(1)
基本的WebGL图形操作(详细参考教程:https://www.yiibai.com/webgl,需要1周左右熟悉webgl的对象方法以及着色器代码):绘制三角形 drawElements gl.TR ...
- WebService 学习笔记(一、概念及定义)
定义 WebService是一种服务导向架构(SOA service-oriented architecture)的技术,通过标准的Web协议提供服务,目的是保证不同平台的应用服务可以互操作. Web ...
- PHP实现长网址与短网址
原文地址:http://www.qqdeveloper.com/detail/29/1.html 什么是长链接.短链接 顾名思义,长链接就是一个很长的链接:短链接就是一个很短的链接.长链接可以生成短链 ...
- I2C驱动
在I2C总线驱动下,也是硬件设备和驱动分离,使以就需要通过它们的名字来匹配,这样驱动的probe函数才能被调用 查看linux内核的Documents目录下的说明文件,可知构造i2c设备有4种方法: ...
- Java学习笔记二十一:Java面向对象的三大特性之继承
Java面向对象的三大特性之继承 一:继承的概念: 继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类. 继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方 ...
- shazam音乐检索算法 附完整c代码
在讲算法之前,上一些前人的资料. http://coding-geek.com/how-shazam-works/ https://laplacian.wordpress.com/2009/01/10 ...
- 快排(golang实现) 递归方法
递归方法,逻辑简洁清晰.这个算法还是很重要的,需要重点记忆理解,面试经常考手写.据说是与傅里叶变换等并称“20世纪十大算法”.https://blog.csdn.net/v_JULY_v/articl ...