LeetCode 27.Remove Element 数组元素删除

简介:

27. Remove Element

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 = 3

Your function should return length = 2, with the first two elements of nums being 2.

题目大意:删除容器中指定的重复元素,然后返回容器的长度。要求不能申请数组来处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class  Solution {
public :
     int  removeElement(vector< int >& nums,  int  val) {
         for ( int  i = 0; i < nums.size(); i++)
         {
             if (nums[i] == val )
             {
                 nums.erase (nums.begin() + i );
                 i--;
             }
         }
         return  nums.size();
     }
};

本文转自313119992 51CTO博客,原文链接:http://blog.51cto.com/qiaopeng688/1834869

相关文章
|
6天前
【力扣】-- 移除链表元素
【力扣】-- 移除链表元素
16 1
|
12天前
【LeetCode 27】347.前k个高频元素
【LeetCode 27】347.前k个高频元素
23 0
|
13天前
【LeetCode-每日一题】 删除排序数组中的重复项
【LeetCode-每日一题】 删除排序数组中的重复项
16 4
|
13天前
|
索引
Leetcode第三十三题(搜索旋转排序数组)
这篇文章介绍了解决LeetCode第33题“搜索旋转排序数组”的方法,该问题要求在旋转过的升序数组中找到给定目标值的索引,如果存在则返回索引,否则返回-1,文章提供了一个时间复杂度为O(logn)的二分搜索算法实现。
12 0
Leetcode第三十三题(搜索旋转排序数组)
|
13天前
|
算法 C++
Leetcode第53题(最大子数组和)
这篇文章介绍了LeetCode第53题“最大子数组和”的动态规划解法,提供了详细的状态转移方程和C++代码实现,并讨论了其他算法如贪心、分治、改进动态规划和分块累计法。
37 0
|
13天前
|
C++
【LeetCode 12】349.两个数组的交集
【LeetCode 12】349.两个数组的交集
13 0
|
13天前
【LeetCode 06】203.移除链表元素
【LeetCode 06】203.移除链表元素
26 0
|
13天前
【LeetCode-每日一题】移除元素
【LeetCode-每日一题】移除元素
26 0
|
28天前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
50 6