您的位置:首页 > 理论基础 > 数据结构算法

LeetCode-27-Remove Element-E

2018-02-03 03:25 399 查看
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 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.

Example:

Given nums = [3,2,2,3], val = 3,

返回一个int类型的整数N,并使得原数组的前N位是所需要的数字。

例子应该返回整数2,代表数组的前两位是所需要的数字,此时的数组为[2,2]。

和26题的基本思路是完全一致的。

int removeElement(vector<int>& nums, int val) {
int sign=0;
for(int i=0;i<nums.size();i++){
if(nums[i]==val)
sign++;
else
nums[i-sign]=nums[i];
}
return nums.size()-sign;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构与算法