LeetCode-27
Links:https://leetcode.com/problems/remove-element/
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.
思路:最快的不是一个个替换,是一次遍历重新做一个Vector替换。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Solution { public: int removeElement(vector<int>& nums, int val) { vector<int>temp; for(int i=0,j=0;i<nums.size();i++) { if(nums[i]!=val) temp.push_back(nums[i]); } nums = temp; return nums.size(); } }; |
【LeetCode】27. Remove Element