From 210604981d59a51f1df8b6bd061c7c669c698917 Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Tue, 15 Nov 2022 15:16:56 +0800 Subject: [PATCH] s0026 --- ...0026_remove_duplicates_from_sorted_array.hpp | 13 +++++++++++++ ...0026_remove_duplicates_from_sorted_array.cpp | 17 +++++++++++++++++ ...0026_remove_duplicates_from_sorted_array.cpp | 17 +++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 include/s0026_remove_duplicates_from_sorted_array.hpp create mode 100644 src/s0026_remove_duplicates_from_sorted_array.cpp create mode 100644 tests/s0026_remove_duplicates_from_sorted_array.cpp diff --git a/include/s0026_remove_duplicates_from_sorted_array.hpp b/include/s0026_remove_duplicates_from_sorted_array.hpp new file mode 100644 index 0000000..e046e01 --- /dev/null +++ b/include/s0026_remove_duplicates_from_sorted_array.hpp @@ -0,0 +1,13 @@ +#ifndef S0026_REMOVE_DUPLICATES_FROM_SORTED_ARRAY +#define S0026_REMOVE_DUPLICATES_FROM_SORTED_ARRAY + +#include + +using namespace std; + +class Solution { + public: + int removeDuplicates(vector& nums); +}; + +#endif diff --git a/src/s0026_remove_duplicates_from_sorted_array.cpp b/src/s0026_remove_duplicates_from_sorted_array.cpp new file mode 100644 index 0000000..6e1e5dd --- /dev/null +++ b/src/s0026_remove_duplicates_from_sorted_array.cpp @@ -0,0 +1,17 @@ +#include "s0026_remove_duplicates_from_sorted_array.hpp" + +int Solution::removeDuplicates(vector& 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; +} diff --git a/tests/s0026_remove_duplicates_from_sorted_array.cpp b/tests/s0026_remove_duplicates_from_sorted_array.cpp new file mode 100644 index 0000000..883b3a7 --- /dev/null +++ b/tests/s0026_remove_duplicates_from_sorted_array.cpp @@ -0,0 +1,17 @@ +#include "s0026_remove_duplicates_from_sorted_array.hpp" + +#include + +TEST(Problem26, Case1) { + vector i{1, 1, 2}; + int o{2}; + Solution solution; + EXPECT_EQ(solution.removeDuplicates(i), o); +} + +TEST(Problem26, Case2) { + vector i{0, 0, 1, 1, 1, 2, 2, 3, 3, 4}; + int o{5}; + Solution solution; + EXPECT_EQ(solution.removeDuplicates(i), o); +}