-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_element.cpp
More file actions
42 lines (32 loc) · 830 Bytes
/
remove_element.cpp
File metadata and controls
42 lines (32 loc) · 830 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
leetcode题目,删除数组中值为target的元素,返回新的长度
* 2.1.11 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.
*
*
*/
#include <vector>
#include <iostream>
using namespace std;
int remove_element(vector<int> &in, int target)
{
int index = 0;
int i;
for (i = 0; i < in.size(); i++)
{
if (in[i] != target) {
in[index++] = in[i];
}
}
return index;
}
int main()
{
int n[] = {1, 3, 5, 6, 4, 7, 5, 3, 4, 5};
vector<int> test(n, n + 10);
int new_len = remove_element(test, 5);
for (int i = 0; i < new_len; i ++) {
cout<<test[i]<<" ";
}
cout<<endl;
}