From 047db7b742bdef0db3f4eba96664c37890e022bf Mon Sep 17 00:00:00 2001 From: SquidSpirit Date: Sat, 15 Feb 2025 00:18:44 +0800 Subject: [PATCH] feat: 26_remove_duplicates_from_sorted_array --- .../main.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 26_remove_duplicates_from_sorted_array/main.cpp diff --git a/26_remove_duplicates_from_sorted_array/main.cpp b/26_remove_duplicates_from_sorted_array/main.cpp new file mode 100644 index 0000000..edfc723 --- /dev/null +++ b/26_remove_duplicates_from_sorted_array/main.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +class Solution { + public: + int removeDuplicates(vector& nums) { + int k = 0, lastNum = nums[0]; + + for (int i = 1; i < nums.size(); i++) { + if (nums[i] == lastNum) { + nums[i] = INT32_MAX; + } else { + lastNum = nums[i]; + } + } + + for (int i = 0; i < nums.size(); i++) { + if (nums[i] != INT32_MAX) { + nums[k++] = nums[i]; + } + } + + return k; + } +};