forked from gongluck/CVIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path27.移除元素.cpp
46 lines (44 loc) · 920 Bytes
/
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
/*
* @lc app=leetcode.cn id=27 lang=cpp
*
* [27] 移除元素
*/
// @lc code=start
class Solution
{
public:
int removeElement(vector<int> &nums, int val)
{
//暴力解法
// int size = nums.size();
// for (int i = 0; i < size; ++i)
// {
// if (nums[i] == val)
// {
// --size;
// for (int j = i; j < size; ++j)
// {
// nums[j] = nums[j + 1];
// }
// --i;
// }
// }
// return size;
//双指针
int fast = 0;
int slow = 0;
while (fast < nums.size())
{
if (nums[fast] != val)
{
nums[slow++] = nums[fast++];
}
else
{
++fast;
}
}
return slow;
}
};
// @lc code=end