s0027
This commit is contained in:
parent
210604981d
commit
381117a18a
13
include/s0027_remove_element.hpp
Normal file
13
include/s0027_remove_element.hpp
Normal file
@ -0,0 +1,13 @@
|
||||
#ifndef S0027_REMOVE_ELEMENT
|
||||
#define S0027_REMOVE_ELEMENT
|
||||
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int removeElement(vector<int>& nums, int val);
|
||||
};
|
||||
|
||||
#endif
|
18
src/s0027_remove_element.cpp
Normal file
18
src/s0027_remove_element.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
#include "s0027_remove_element.hpp"
|
||||
|
||||
int Solution::removeElement(vector<int>& nums, int val) {
|
||||
int size = nums.size();
|
||||
if (size == 0) {
|
||||
return 0;
|
||||
}
|
||||
int fast{0}, slow{0};
|
||||
for (; fast < size; ++fast) {
|
||||
if (nums.at(fast) == val) {
|
||||
continue;
|
||||
} else {
|
||||
nums[slow] = nums.at(fast);
|
||||
++slow;
|
||||
}
|
||||
}
|
||||
return slow;
|
||||
}
|
19
tests/s0027_remove_element.cpp
Normal file
19
tests/s0027_remove_element.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include "s0027_remove_element.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(Problem27, Case1) {
|
||||
vector<int> nums{3, 2, 2, 3};
|
||||
int val{3};
|
||||
int o{2};
|
||||
Solution solution;
|
||||
EXPECT_EQ(solution.removeElement(nums, val), o);
|
||||
}
|
||||
|
||||
TEST(Problem27, Case2) {
|
||||
vector<int> nums{0, 1, 2, 2, 3, 0, 4, 2};
|
||||
int val{2};
|
||||
int o{5};
|
||||
Solution solution;
|
||||
EXPECT_EQ(solution.removeElement(nums, val), o);
|
||||
}
|
Loading…
Reference in New Issue
Block a user