-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path27.cpp
More file actions
26 lines (26 loc) · 748 Bytes
/
27.cpp
File metadata and controls
26 lines (26 loc) · 748 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Author: btjanaka (Bryon Tjanaka)
// Problem: (Leetcode) 27
// Title: Remove Element
// Link: https://leetcode.com/problems/remove-element
// Idea: Move all the values to the end of the array by swapping the item at
// the current index with the item at a "back pointer" at the end of the array.
// Difficulty: easy
// Tags: implementation
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int back_ptr = nums.size() - 1;
for (int i = 0; i <= back_ptr; ++i) {
while (back_ptr >= 0 && nums[i] == val) {
if (i != back_ptr) {
swap(nums[i], nums[back_ptr]);
--back_ptr;
} else {
--back_ptr;
break;
}
}
}
return back_ptr + 1;
}
};