Skip to content

Instantly share code, notes, and snippets.

@qiaoxu123
Last active November 18, 2018 02:03
Show Gist options
  • Save qiaoxu123/ceb5ca6946aebb45357f64874e01da4b to your computer and use it in GitHub Desktop.
Save qiaoxu123/ceb5ca6946aebb45357f64874e01da4b to your computer and use it in GitHub Desktop.
Given an array nums and a value val, 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 e
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
if(nums.empty()) return 0;
int len = 0;
for(int i = 0;i < nums.size();++i){
if(nums[i] != val)
nums[len++] = nums[i];
}
return len;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment