Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
分析:给你一个数组和一个具体的值,删除掉数组中的这个值,并且返回新的长度。
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int index;
index=0;
for(int i=0;i<nums.size();i++)
{
if(nums[i]!=val)
nums[index++]=nums[i];
}
return index;
}
};
本文介绍了一种高效的方法来删除数组中特定值的所有实例,并返回更新后的数组长度。通过遍历数组并忽略指定值的方式,实现了原地修改,简化了操作流程。
650

被折叠的 条评论
为什么被折叠?



