forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path27.cpp
More file actions
37 lines (35 loc) · 726 Bytes
/
27.cpp
File metadata and controls
37 lines (35 loc) · 726 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
// First Approach
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
for(auto it=nums.begin();it!=nums.end();it++)
{
if(*it==val)
{
nums.erase(it);
--it;
}
}
return nums.size();
}
};
//Using stacks
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int n=nums.size();
stack<int> s;
for(int i=0;i<n;i++)
{
if(nums[i]!=val)
s.push(nums[i]);
}
nums.clear();
while(!s.empty())
{
nums.push_back(s.top());
s.pop();
}
return nums.size();
}
};