27. Remove Element[E]移除元素
题目
Given an array nums and a value val, 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 by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
**Example1: **
Given nums = [3,2,2,3], val = 3,
Your function should return length = 2, with the first two elements of nums being 2.
It doesn't matter what you leave beyond the returned length.
**Example2: **
Given nums = [0,1,2,2,3,0,4,2], val = 2,
Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
Note that the order of those five elements can be arbitrary.
It doesn't matter what values are set beyond the returned length.
思路
题目要求不使用额外的空间,移除所有数值等于val的元素,返回新数组的长度,于是想到使用替换的方法。
将数组中数值等于val的元素与数组最后一个元素互换,再将最后一个元素删除。这样就完成了一个删除数值等于val元素的操作。
Tips
STL中的容器vector
(1)声明以及初始化
//声明一个int类型的向量vec
vector<int> vec;
//声明一个int类型,初始大小为10的向量vec
vector<int> vec(10);
//声明一个int类型,初始大小为10,且元素都为0的向量vec
vector<int> vec(10, 0);
//声明一个int类型的向量vec,并用向量vec1初始化vec
vector<int> vec(vec1);
// 利用数组arr的前五个元素初始化向量vec
int arr[5] = {0, 1, 2, 3, 4};
vector<int> vec(arr, arr + 5);
(2)元素访问
//下标访问,不会进行越界检查
vec[0]
//at访问,会检查越界,如果出界会抛出out of range 异常
vec.at(0)
//访问第一个元素
vec.front()
//访问最后一个元素
vec.back()
(3)元素修改
//在int类型的向量vec的末尾添加一个int元素1
vec.push_back(1);
//删除vec的最后一个元素
vec.pop_back();
//在vec的pos位置插入元素element
vec.insert(pos, element);
//将向量vec中pos1位置与pos2位置的元素互换(swap可以用来释放内存)
vec.swap(pos1, pos2);
//删除向量vec中pos位置的元素
vec.erase(pos);
//删除向量vec中pos1位置到pos2位置的元素
vec.erase(pos1, pos2);
//清空向量vec
vec.clear();
(4)元素容量
在容器vector中,其内存占用的空间是只增不减的,比如说首先分配了10,000个字节,然后erase掉后面9,999个,则虽然有效元素只有一个,但是内存占用仍为10,000个。所有内存空间在vector析构时回收。一般,我们都会通过vector中成员函数clear进行一些清除操作,但它清除的是所有的元素,使vector的大小减少至0,却不能减小vector占用的内存。
//向量vec的大小,它告诉你容器里有多少元素。
vec.size()
//向量vec的分配容量,可以大于size。它告诉你容器在已经分配的内存中总共可以容纳多少元素。
vec.capacity()
//释放int类型的向量vec的内存
vector<int>().swap(vec);
//降低vec的容量,使其与size匹配(可用于删除元素后,节省向量的空间)
vec.shrink_to_fit()
//为容器预留空间,但在空间内不真正创建元素对象,所以在没有添加新的对象之前,不能引用容器内的元素。
//使用push_back添加新元素
vec.reserve(10);
for(int i = 0; i < 10; i++)
vec.push_back(1);
//改变容器的大小,同时创建对象。
//resize有两个参数,第一个是容器大小,第二个参数时新加入容器的元素。
//调用这个函数之后,可以使用[]添加新元素。
vec.resize(10);
vec[0] = 0; //将第一个元素赋值为0
C++
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
for(int i=0;i<nums.size();i++){
if(nums[i] == val){
nums[i] = nums.back();
nums.pop_back();
//对交换来的数组最后一个元素也进行检查,防止数组最后一个元素数值也等于val
i--;
}
}
return nums.size();
}
};
Python
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
while val in nums:
nums.pop(nums.index(val))
return len(nums)
27. Remove Element[E]移除元素的更多相关文章
- LeetCode 27. Remove Element (移除元素)
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- 27. Remove Element C++移除元素
网址:https://leetcode.com/problems/remove-element/ 双指针(广义) class Solution { public: int removeElement( ...
- LeetCode 27 Remove Element (移除数组中指定元素)
题目链接: https://leetcode.com/problems/remove-element/?tab=Description Problem : 移除数组中给定target的元素,返回剩 ...
- LeetCode OJ:Remove Element(移除元素)
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- 27. Remove Element【easy】
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...
- leetCode练题——27. Remove Element
1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...
- leetCode 27.Remove Element (删除元素) 解题思路和方法
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
随机推荐
- 【SQL】字符型函数
1. ASCII ASCII(American Standard Code for Information Interchange,美国信息交换标准代码)是基于拉丁字母的一套电脑编码系统. 1) 返回 ...
- C# 查找、结束进程 - 通过进程名精确、模糊查找、结束进程
/// <summary> /// 根据“精确进程名”结束进程 /// </summary> /// <param name="strProcName" ...
- JAVA可能问的几个面试题问题及问题的标准答案
问题一:你希望工作环境是怎样的? 标准答案:我对环境没有苛求,我会努力适应环境的. 注:此问题是在测试你的求职心态,是以自己为中心还是以工作为中心. 问题二:你觉得自己有哪些缺点? 标准答案:说一些对 ...
- 存档:Telerik Test Studio的摸索笔记
http://www.51testing.com/?uid-170604-action-spacelist-starttime-1328025600-endtime-1330531200 http:/ ...
- AI不与人为敌
业界广为流传着一句话:有多少人工就有多少智能. 其实还应该有一句话:有多少付出就因该有多少回报.公正是世界永恒的话题. 一.人工智能还是人工愚蠢 科技从来没有善恶,也不会杀人,愚蠢的人比聪明的人做的错 ...
- OAuth密码模式说明(resource owner password credentials)
用户向客户端(third party application)提供用户名和密码. 客户端将用户名和密码发给认证服务器(Authorization server),向后者请求令牌(token). 认证服 ...
- shell脚本—基础知识,变量
shell脚本本质: 编译型语言 解释型语言 shell编程基本过程 1.建立shell文件 2.赋予shell文件执行权限,使用chmod命令修改权限 3.执行shell文件 shell变量: sh ...
- [转]理解和配置 Linux 下的 OOM Killer
最近有位 VPS 客户抱怨 MySQL 无缘无故挂掉,还有位客户抱怨 VPS 经常死机,登陆到终端看了一下,都是常见的 Out of memory 问题.这通常是因为某时刻应用程序大量请求内存导致系统 ...
- DataInputStream 数据类型数据输入输出流
package IOliu; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileI ...
- 【例题4-6 uva12412】A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 训练编程的题. 原题中没有除0的数据,所以别担心你的代码是因为除0错了. 多半跟我一样. 也是因为没有+eps 就是比如你要算tot ...