-
Notifications
You must be signed in to change notification settings - Fork 0
/
27-RemoveElement.cpp
51 lines (44 loc) · 994 Bytes
/
27-RemoveElement.cpp
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
47
48
49
50
51
/*
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.
*/
#include <stdio.h>
#include <vector>
using namespace std;
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
if (nums.empty()) {
return 0;
}
int count = 0;
for (size_t i = 0; i < nums.size(); i++) {
if (nums[i] != val) {
nums[count] = nums[i];
count++;
} else {
continue;
}
}
return count;
}
};
int main() {
int array[8] = {1, 1, 2, 5, 5, 5, 6, 8};
vector <int> source;
for (int i = 0; i < 8; i++) {
source.push_back(array[i]);
}
for (size_t i = 0; i < source.size(); i++) {
printf("%d ", source[i]);
}
printf("\n");
Solution s;
int ret = s.removeElement(source, 5);
for (size_t i = 0; i < source.size(); i++) {
printf("%d ", source[i]);
}
printf("\n");
printf("Return value is %d\n", ret);
return 0;
}