博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 27. Remove Element
阅读量:4098 次
发布时间:2019-05-25

本文共 821 字,大约阅读时间需要 2 分钟。

题目:

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.

思路:给定一个vector和一个整数,删除vector中与这个整数相等的所有数的位置,并返回新vector的大小

代码:

class Solution {public:	int removeElement(vector
& nums, int val) { vector
::iterator iter = nums.begin(); while (iter != nums.end()){ if (val == *iter){//当前位置的数如果和给定的val相等,则用erase()删除,并返回当前位置下一个位置的iterator赋给iter iter = nums.erase(iter); } else{//如果不相等,则iter递增 ++iter; } } return nums.size(); }};

转载地址:http://ynmii.baihongyu.com/

你可能感兴趣的文章
realsense-ros里里程计相关代码
查看>>
似乎写个ROS功能包并不难,你会订阅话题发布话题,加点逻辑处理,就可以写一些基础的ROS功能包了。
查看>>
我觉得在室内弄无人机开发装个防撞机架还是很有必要的,TBUS就做得很好。
查看>>
serial也是见到很多次了,似乎它就是一种串行通信协议
查看>>
TBUS的一些信息
查看>>
专业和业余的区别就在于你在基础在基本功打磨练习花的时间
查看>>
通过mavlink实现自主航线的过程笔记
查看>>
这些网站有一些嵌入式面试题合集
查看>>
我觉得刷题是有必要的,不然小心实际被问的时候懵逼,我觉得你需要刷个50份面试题。跟考研数学疯狂刷卷子一样!
查看>>
Git操作清单
查看>>
Flutter Boost的router管理
查看>>
ES7 await/async
查看>>
ES7的Async/Await
查看>>
每个人都能做的网易云音乐[vue全家桶]
查看>>
Vue2.0全家桶仿腾讯课堂(移动端)
查看>>
React+Redux系列教程
查看>>
19 个 JavaScript 常用的简写技术
查看>>
iOS应用间相互跳转
查看>>
iOS开发 支付之银联支付集成
查看>>
iOS开发支付集成之微信支付
查看>>