first init
This commit is contained in:
39
array/1binary search/27remove_target/src/27.cpp
Normal file
39
array/1binary search/27remove_target/src/27.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int removeElement(vector<int>& nums, int val) {
|
||||
|
||||
int count = nums.size();
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (nums[i] != val)
|
||||
{
|
||||
nums[slow_index] = nums[i];
|
||||
slow_index++;
|
||||
}
|
||||
}
|
||||
return slow_index;
|
||||
|
||||
}
|
||||
private:
|
||||
|
||||
int slow_index = 0;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<int> nums = {0, 1, 2, 2, 3, 0, 4, 2};
|
||||
int val = 2;
|
||||
Solution solution;
|
||||
int result = solution.removeElement(nums, val);
|
||||
cout << "The length of nums after removing val is: " << result << endl;
|
||||
for (int i = 0; i < result; i++)
|
||||
{
|
||||
cout << nums[i] << " ";
|
||||
}
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user