27.Remove Element
√ Accepted
√ 113/113 cases passed (8 ms)
√ Your runtime beats 37.73 % of cpp submissions
√ Your memory usage beats 100 % of cpp submissions (8.5 MB)
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int i = 0;
int j = nums.size() - 1;
int repeat = 0;
while (i <= j)
{
if (nums[i] == val && nums[j] != val)
{
nums[i] = nums[j];
i++;
j--;
repeat += 1;
}
else if (nums[i] == val && nums[j] == val)
{
j--;
repeat += 1;
}
else
{
i++;
}
}
return nums.size() - repeat;
}
};