Commit d819cdd6 authored by yaozc's avatar yaozc
Browse files

26 solved

parent 778ccbab
......@@ -28,3 +28,19 @@ ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
return l2;
}
}
//26 Remove Duplicates from Sorted Array
//solution1: use vector earse function
//solution2 (following one): left and right pointer, once nums[left] is less than nums[right] swap them so nums[left] is the largest distinct element so far. Hard to think but elegent
int removeDuplicates(vector<int>& nums) {
if (nums.size() < 2){
return nums.size();
}
int left = 0;
for(int right=1; right<nums.size(); right++){
if(nums[left] < nums[right]){
swap(nums[++left], nums[right]);
}
}
return ++left;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment