-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremoveElement.cpp
More file actions
46 lines (39 loc) · 1.12 KB
/
removeElement.cpp
File metadata and controls
46 lines (39 loc) · 1.12 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <vector>
using namespace std;
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
// [ 1 1 2 2 5 5 ]
// remove all 2s
if (!nums.size()) { return nums.size(); }
int end = nums.size() - 1;
for (int i = 0; i < nums.size(); i++) {
// get the array end index which is not val
if (end <= i) { end++; printf("break \n"); goto exit; }
while (nums[end] == val) {
if (end <= i) { printf("break \n"); goto exit; }
printf("END -- ");
end--;
}
if (nums[i] == val) {
printf("swapping %d %d %d %d \n", nums[i], i, nums[end], end);
swap(nums[i], nums[end]);
}
}
exit:
printf("SSSSS %d %d \n", nums.size(), end);
if (end == nums.size() - 1) {
return nums.size();
} else {
return nums.size() - end;
}
}
};
int main()
{
Solution s;
vector<int> nums = { 4, 5};
int ans = s.removeElement(nums, 4);
printf("size %d \n", ans);
return 0;
}