【LEET-CODE】27. Remove Element
来源:互联网 发布:python 多线程 返回值 编辑:程序博客网 时间:2024/06/04 17:47
Qusetion:
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.
思路:
删除数组中的val,循环比较即可,注意使用erase后使用i--调整标号
Code:
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--; } } int length = nums.size(); return length; }};
Your runtime beats 76.31% of cpp submissions
0 0
- 【LEET-CODE】27. Remove Element
- [leet code] Remove Element
- Leet Code OJ 27. Remove Element [Difficulty: Easy]
- Majority Element (Leet Code)
- leet code 第25-26题Remove Duplicates from Sorted Array & Remove Element
- CODE 105: Remove Element
- 【Leet Code】229. Majority Element II---Medium
- [leet code] Remove Duplicates from Sorted List
- [leet code] Remove Duplicates from Sorted Array
- Leet Code OJ 169. Majority Element [Difficulty: Easy]
- [leet code] Remove Duplicates from Sorted Array I & II
- [leet code] Remove Nth Node From End of List
- [leet code] Remove Duplicates from Sorted List II
- Leet Code 19 Remove Nth Node From End of List
- 【Leet Code】26. Remove Duplicates from Sorted Array---Easy
- 【Leet Code】80. Remove Duplicates from Sorted Array II---Medium
- Leet Code OJ 203. Remove Linked List Elements [Difficulty: Easy]
- 【LEET-CODE】26. Remove Duplicates from Sorted Array
- git 简单使用笔记
- GitHub上史上最全的Android开源项目分类汇总
- 实体类为啥要序列化
- shareSDK使用
- HTML5基础加强css样式篇(伸缩容器属性:flex-direction, flex-wrap,flex-flow,align-items,align-content)(五十三)
- 【LEET-CODE】27. Remove Element
- 通过LoadLibrary() 与 GetProcAddress()获得Dll内的函数
- iOS js和OC方法交互
- CSU 1896: Symmetry(对称点集)
- H.264编码下直播视频添加水印的优化
- Mac版R语言(四)网络数据自动获取
- Java中的23种设计模式
- 键盘Keycode对照表
- 程序员什么时候该考虑辞职?