This commit is contained in:
Sainnhe Park 2022-11-15 15:16:56 +08:00
parent 5f1b40adc4
commit 210604981d
3 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,13 @@
#ifndef S0026_REMOVE_DUPLICATES_FROM_SORTED_ARRAY
#define S0026_REMOVE_DUPLICATES_FROM_SORTED_ARRAY
#include <vector>
using namespace std;
class Solution {
public:
int removeDuplicates(vector<int>& nums);
};
#endif

View File

@ -0,0 +1,17 @@
#include "s0026_remove_duplicates_from_sorted_array.hpp"
int Solution::removeDuplicates(vector<int>& nums) {
int size = nums.size();
if (size == 0 || size == 1) {
return size;
}
int slow{0};
for (int fast{1}; fast < size; ++fast) {
if (nums.at(fast) == nums.at(slow)) {
continue;
} else {
nums[++slow] = nums.at(fast);
}
}
return slow + 1;
}

View File

@ -0,0 +1,17 @@
#include "s0026_remove_duplicates_from_sorted_array.hpp"
#include <gtest/gtest.h>
TEST(Problem26, Case1) {
vector<int> i{1, 1, 2};
int o{2};
Solution solution;
EXPECT_EQ(solution.removeDuplicates(i), o);
}
TEST(Problem26, Case2) {
vector<int> i{0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
int o{5};
Solution solution;
EXPECT_EQ(solution.removeDuplicates(i), o);
}