From 28dbc298bc0ee43bb8459c5b20651553bd13eca7 Mon Sep 17 00:00:00 2001 From: huihut Date: Thu, 15 Feb 2018 01:27:59 +0800 Subject: [PATCH] Update 26. Remove Duplicates from Sorted Array --- .../26-remove-duplicates-from-sorted-array.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Problems/LeetcodeProblems/26-remove-duplicates-from-sorted-array.h b/Problems/LeetcodeProblems/26-remove-duplicates-from-sorted-array.h index e69de29..2ce7bb6 100644 --- a/Problems/LeetcodeProblems/26-remove-duplicates-from-sorted-array.h +++ b/Problems/LeetcodeProblems/26-remove-duplicates-from-sorted-array.h @@ -0,0 +1,14 @@ +class Solution { +public: + int removeDuplicates(vector& nums) { + int n = nums.size(), pos = 0; + if(n <= 1) + return n; + for(int i = 0; i < n-1; ++i) { + if(nums[i] != nums[i+1]) { + nums[++pos] = nums[i+1]; + } + } + return pos+1; + } +}; \ No newline at end of file -- GitLab