【题目描述】
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.
【思路】水题一道,思路跟Remove Duplicates from Sorted Array类似。
【代码】
class Solution { public: int removeElement(vector<int>& nums, int val) { int n=nums.size(); if(n==0) return n; int ans=0; for(int i=0;i<n;i++){ if(nums[i]!=val){ nums[ans]=nums[i]; ans++; } } nums.resize(ans); return ans; } };