initial commit

This commit is contained in:
wt
2026-04-08 15:12:07 +07:00
commit 942e4e2dc1
30 changed files with 297 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
executable('rmdupfromsortedarray2', 'src/main.cpp', install: true)
+15
View File
@@ -0,0 +1,15 @@
#include "rmdufromsortedarray2.hpp"
#include <cassert>
#include <vector>
int main() {
std::vector<int> nums = {1, 1, 1, 2, 2, 3};
std::vector<int> expectednums = {1, 1, 2, 2, 3};
int k = removeDuplicates(nums);
assert(k == int(expectednums.size()));
for (int i = 0; i < k; i++)
{
assert(nums[i] == expectednums[i]);
}
return 0;
}
@@ -0,0 +1,14 @@
#include <vector>
inline int removeDuplicates(std::vector<int> &nums) {
const int n = nums.size();
if (n <= 2) return n;
int i = 2;
for (int j = 2; j < n; ++j) {
if (nums[j] != nums[i - 2]) [[likely]] {
nums[i++] = nums[j];
}
}
return i;
}