【一天一道LeetCode】#27. Remove Element
一天一道LeetCode系列
(一)题目
Given an array and a value, remove all instances of that value in place and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
Example:
Given input array nums = [3,2,2,3], val = 3Your function should return length = 2, with the first two elements of nums being 2.
(二)解题
解题思路见注释
/*
在一个vector中删除指定的元素,用到erase()函数,注意迭代器会失效
erase()会返回被删除元素的下一个元素的迭代器
*/
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
for(auto iter = nums.begin();iter!=nums.end();){
if(*iter == val){
iter = nums.erase(iter);//iter指向被删除元素的下一个元素
}
else
++iter;
}
return nums.size();
}
};
【一天一道LeetCode】#27. Remove Element的更多相关文章
- leetCode 27.Remove Element (删除元素) 解题思路和方法
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- [LeetCode] 27. Remove Element 移除元素
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- LeetCode 27. Remove Element (移除元素)
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- [LeetCode] 27. Remove Element ☆
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- LeetCode 27 Remove Element
Problem: Given an array and a value, remove all instances of that value in place and return the new ...
- Leetcode 27 Remove Element STL
和remove zero类似的方法完成该题 class Solution { public: int removeElement(vector<int>& nums, int va ...
- Java [leetcode 27]Remove Element
题目描述: Given an array and a value, remove all instances of that value in place and return the new len ...
- Leetcode 27——Remove Element
Given an array and a value, remove all instances of that value in-place and return the new length. D ...
- (双指针) leetcode 27. Remove Element
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- Leetcode 27. Remove Element(too easy)
Given an array and a value, remove all instances of that value in-place and return the new length. D ...
随机推荐
- 高仿腾讯QQ最终版
之前写过一篇关于高仿腾讯QQ的博客,不知道的看这:http://blog.csdn.net/htq__/article/details/51840273 ,主要是从界面上高仿了腾讯QQ,在UI上基本上 ...
- [OpenCV] GpuMat and Mat, compare cvtColor perforemence
Introduction I am going to measure the performence of my two GT650M and compare GPU with CPU version ...
- [OpenCV]拓展图像边界
图像处理中经常遇到使用当前像素邻的像素来计算当前像素位置的某些属性值,这样就会导致边界像素处越界访问,一般有两种方法解决这种问题:只对不越界的像素进行处理:对图像边界进行拓展,本文主要介绍如何使用Op ...
- Android图表库MPAndroidChart(十)——散点图的孪生兄弟气泡图
Android图表库MPAndroidChart(十)--散点图的孪生兄弟气泡图 起泡图和散点图如出一辙,但是个人认为要比散点图好看一点,我们来看下实际的演示效果 这个和散点图的实现很相似,我们一起来 ...
- 23 广播服务结合音乐Demo5
MainActivity.java package com.dmy.demo5; import android.app.Activity; import android.content.Broadca ...
- C++ 虚函数表 单继承
本文研究单继承情况下,c++对象的虚函数表的具体情况. 假设有两个类A,B, 其中B由A派生出来,A含有虚函数fun1,B含有虚函数fun2. 测试的代码如下: #include<iostrea ...
- Dynamics CRM2013 Odata的filter中含有日期字段时遇到的一个奇葩问题
在使用Odata拼写filter时我们一般都用工具,因为手写是件极不靠谱且错误率极高的事,下图是我用query designer拼出来的一个filter,因为时间是参数,所以在拷贝出下面这段filte ...
- 这一次,VR离我们真的很近
从高考作文开始 今年号称是VR元年,虽然目前VR还没能像手机一样走进千家万户,但关于VR设备的关讨论是层出不穷.而今年高考,浙江省的作文题就与VR相关.网上购物.视频聊天等在 ...
- 在Linux上的虚拟机上启动Oracle上报ORA-00845: MEMORY_TARGET not supported on this system的问题解决
解决办法: 1.将当前虚拟机的内容调整大一些(以下转载:http://jingyan.baidu.com/article/414eccf67b8baa6b421f0a60.html) VMware虚拟 ...
- studio中碰到的jni问题:java.lang.UnsatisfiedLinkError
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52606328 studio中碰到 ...