Last active
November 18, 2018 02:03
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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