forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain1.cpp
More file actions
40 lines (27 loc) · 680 Bytes
/
Copy pathmain1.cpp
File metadata and controls
40 lines (27 loc) · 680 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
/// Source : https://leetcode.com/problems/remove-element/
/// Author : liuyubobobo
/// Time : 2016-12-05
#include <iostream>
#include <vector>
#include <cassert>
#include <stdexcept>
using namespace std;
/// Two Pointers
///Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int newl = 0;
for( int i = 0 ; i < nums.size() ; i ++ )
if( nums[i] != val )
nums[newl++] = nums[i];
return newl;
}
};
int main() {
vector<int> nums = {3, 2, 2, 3};
int val = 3;
cout << Solution().removeElement(nums, val) << endl;
return 0;
}