LeetCode
来源:互联网 发布:js this对象 编辑:程序博客网 时间:2024/05/22 16:56
原题网址:https://leetcode.com/problems/remove-element/#/description
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.
题解:
遍历数组,当nums[i] = val时,删除这个数,最后返回数组长度即可。删除和返回数组长度都可以直接用vector中的函数。
非常简单,所以直接给出代码:
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 -= 1; } } return nums.size(); }};
0 0
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- leetcode
- C++中的IO库
- 蓝桥杯-队列操作(java)
- bash笔记(2)
- 关于AS用点9图时遇到的错误的解决方法Error:Execution failed for task ':app:mergeDebugResources'
- 电路笔记
- LeetCode
- sgd与momentum等训练参数方案参考网址
- Vim插件之vim-airline
- EL表达式和JSTL标签库
- 299美元企业级账号申请过程
- L1-035. 情人节
- 二叉搜索树实现简单字典
- ViewPager切换防止Fragment销毁以及取消Fragment的预加载
- 批量导入不受信任的证书及软件限制策略的应用