We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents f8d4f6d + d7f5592 commit 1b9100eCopy full SHA for 1b9100e
leetcode/cpp/Array/27.cpp
@@ -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
19
20
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
36
37
0 commit comments