summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-05-18 15:07:46 +0800
committercrupest <crupest@outlook.com>2020-05-18 15:07:46 +0800
commitff61b61a78e5ac3d510e8221d311186a3389df50 (patch)
tree8d5696248ddd8fc3a5e0c188455ac1d1575232f1
parent38576346da544bd44fc58b65a0a17d9a0cf80130 (diff)
downloadsolutions-ff61b61a78e5ac3d510e8221d311186a3389df50.tar.gz
solutions-ff61b61a78e5ac3d510e8221d311186a3389df50.tar.bz2
solutions-ff61b61a78e5ac3d510e8221d311186a3389df50.zip
Add problem 26 .
-rw-r--r--cpp/26.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/cpp/26.cpp b/cpp/26.cpp
new file mode 100644
index 0000000..3ee4aa7
--- /dev/null
+++ b/cpp/26.cpp
@@ -0,0 +1,37 @@
+#include <vector>
+
+using std::vector;
+
+class Solution
+{
+public:
+ int removeDuplicates(vector<int> &nums)
+ {
+ if (nums.empty())
+ return 0;
+
+ auto iter_head = nums.cbegin();
+ auto iter = iter_head + 1;
+ int current = nums.front();
+ int count = 1;
+
+ while (iter != nums.cend())
+ {
+ const auto v = *iter;
+ if (v == current)
+ {
+ nums.erase(iter);
+ iter = iter_head + 1;
+ }
+ else
+ {
+ current = v;
+ count++;
+ iter_head = iter;
+ ++iter;
+ }
+ }
+
+ return count;
+ }
+};