-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
27.cpp
118 lines (114 loc) · 2.98 KB
/
27.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
__________________________________________________________________________________________________
4ms
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int tmp_size = nums.size();
int i = 0;
while(i < tmp_size)
{
if(nums[i] == val)
{
nums.erase(nums.begin()+i, nums.begin()+i+1);
tmp_size -= 1;
}
else{
i++;
}
}
return tmp_size;
}
};
__________________________________________________________________________________________________
8ms
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
if(nums.size() == 0)
return 0;
int ptr1, ptr2;
for(ptr1=0,ptr2=0; ptr1 < nums.size(); ptr1++){
if(nums[ptr1] != val){
nums[ptr2++] = nums[ptr1];
}
}
return ptr2;
}
};
__________________________________________________________________________________________________
8652 kb
static int x = []() {
std::ios::sync_with_stdio( false );
std::cin.tie( NULL );
std::cout.tie( NULL );
return NULL;
}();
class Solution {
public:
int removeElement( vector<int> &nums, int val ) {
int i = 0;
int j = nums.size() - 1;
int result = 0;
for ( i = 0; i < nums.size(); ++i ) {
if ( nums[ i ] == val ) {
while ( j > i ) {
if ( nums[ j ] != val ) {
swap( nums[ i ], nums[ j ] );
result++;
j--;
break;
}
j--;
}
} else {
result++;
}
}
return result;
}
void swap( int &a, int &b ) {
int temp = a;
a = b;
b = temp;
}
};
__________________________________________________________________________________________________
8668 kb
static const auto __ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return nullptr;
}();
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int len = nums.size();
if (len == 0) return 0;
int start = -1;
for (int i = 0; i < len; ++i) {
if (nums[i] != val) {
++start;
if (start != i) swap(nums[start], nums[i]);
}
}
return start + 1;
}
};
__________________________________________________________________________________________________
8768 kb
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int update = 0, len = nums.size();
for (int i=0; i<len; i++){
if (nums[i]!=val){
nums[update] = nums[i];
update++;
}
}
return update;
}
};
__________________________________________________________________________________________________