题目

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]移除元素的更多相关文章

  1. LeetCode 27. Remove Element (移除元素)

    Given an array and a value, remove all instances of that value in place and return the new length. D ...

  2. 27. Remove Element C++移除元素

    网址:https://leetcode.com/problems/remove-element/ 双指针(广义) class Solution { public: int removeElement( ...

  3. LeetCode 27 Remove Element (移除数组中指定元素)

    题目链接: https://leetcode.com/problems/remove-element/?tab=Description   Problem : 移除数组中给定target的元素,返回剩 ...

  4. LeetCode OJ:Remove Element(移除元素)

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  5. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  6. [Leetcode][Python]27: Remove Element

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...

  7. 27. Remove Element【easy】

    27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...

  8. leetCode练题——27. Remove Element

    1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...

  9. leetCode 27.Remove Element (删除元素) 解题思路和方法

    Remove Element Given an array and a value, remove all instances of that value in place and return th ...

随机推荐

  1. 【SQL】INTERVAL YEAR TO MONTH 和 INTERVAL DAY TO SECOND

    INTERVAL YEAR TO MONTH: 作为年和月的时间间隔存储 INTERVAL DAY TO SECOND: 作为天.小时.分和秒的时间间隔存储(DAY,HOUR,MINUTE,SECON ...

  2. java就业前景发展方向分析

    随着信息化的发展,IT培训受倒了越来越多人的追捧.在开发领域,JAVA培训成为了许多人的首选!java拥有强大的开发者的数量已超过了之前的900万,将近97%的企业电脑也在运行着java,其下载量每年 ...

  3. DNN:windows使用 YOLO V1,V2

    本文有修改,如有疑问,请移步原文. 原文链接:  YOLO v1之总结篇(linux+windows) 此外:  YOLO-V2总结篇   Yolo9000的改进还是非常大的 由于原版的官方YOLOv ...

  4. ROS:使用ubuntuKylin17.04安装ROS赤xi龟

    使用ubuntuKylin17.04安装 参考了此篇文章:SLAM: Ubuntu16.04安装ROS-kinetic 重复官方链接的步骤也没有成功. 此后发现4.10的内核,不能使用Kinetic. ...

  5. boost::mutex::scoped_lock

    在三维重建过程中,世界地图 Map &world作为唯一 访问/更新 对象,可以使用boost::mutex::scoped_lock . 一:boost::mutex::scoped_loc ...

  6. dispatch_sync

    dispatch_sync does two things: queue a block blocks the current thread until the block has finished ...

  7. element ui table(表格)点击一行展开

    element ui是一个非常不错的vue的UI框架,element对table进行了封装,简化了vue对表格的渲染. element ui表格中有一个功能是展开行,在2.0版本官网例子中,只可以点击 ...

  8. PAT_A1120#Friend Numbers

    Source: PAT A1120 Friend Numbers (20 分) Description: Two integers are called "friend numbers&qu ...

  9. 【剑指Offer】18、二叉树的镜像

      题目描述:   操作给定的二叉树,将其变换为原二叉树的镜像.   解题思路:   求一棵树的镜像的过程:先前序遍历这棵树的每个结点,如果遍历到的结点有子结点,就交换它的两个子结点.当交换完所有的非 ...

  10. Redis学习笔记(一)-持久化

    一.RDB持久方式 RDB持久化是把当前进程的数据已快照的形式保存到硬盘的过程. 触发方式: 1.手动触发命令:save和bgsave save:阻塞式,内存较大的实例在执行过程中会造成长时间的阻塞, ...