s0026
This commit is contained in:
parent
5f1b40adc4
commit
210604981d
13
include/s0026_remove_duplicates_from_sorted_array.hpp
Normal file
13
include/s0026_remove_duplicates_from_sorted_array.hpp
Normal 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
|
17
src/s0026_remove_duplicates_from_sorted_array.cpp
Normal file
17
src/s0026_remove_duplicates_from_sorted_array.cpp
Normal 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;
|
||||
}
|
17
tests/s0026_remove_duplicates_from_sorted_array.cpp
Normal file
17
tests/s0026_remove_duplicates_from_sorted_array.cpp
Normal 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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user