Skip to content

Commit 1b9100e

Browse files
authored
Merge pull request #78 from GuptaTanisha/master
Create 27.cpp
2 parents f8d4f6d + d7f5592 commit 1b9100e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

leetcode/cpp/Array/27.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// First Approach
2+
class Solution {
3+
public:
4+
int removeElement(vector<int>& nums, int val) {
5+
for(auto it=nums.begin();it!=nums.end();it++)
6+
{
7+
if(*it==val)
8+
{
9+
nums.erase(it);
10+
--it;
11+
}
12+
}
13+
return nums.size();
14+
}
15+
};
16+
17+
//Using stacks
18+
class Solution {
19+
public:
20+
int removeElement(vector<int>& nums, int val) {
21+
22+
int n=nums.size();
23+
stack<int> s;
24+
for(int i=0;i<n;i++)
25+
{
26+
if(nums[i]!=val)
27+
s.push(nums[i]);
28+
}
29+
nums.clear();
30+
while(!s.empty())
31+
{
32+
nums.push_back(s.top());
33+
s.pop();
34+
}
35+
return nums.size();
36+
}
37+
};

0 commit comments

Comments
 (0)