Remove duplicates from Sorted arraymy submissionsQuestionTotal Accepted: 104150 Total Submissions: 322188 Difficulty: Easy
Given a sorted array, remove the duplicates in place such, all element appear only once and return the new L Ength.
Do the allocate extra space for another array, and you must does this on place with constant memory.
For example,
Given input array nums = [1,1,2]
,
Your function should return length = 2
, with the first of the elements of nums being and 1
2
Respectivel Y. It doesn ' t matter what are you leave beyond the new length.
Subscribe to see which companies asked this question
Hide TagsArray of Pointers
First of all: he's already said ordered, so it's simple class solution {Public:int removeduplicates (vector<int> &nums) { if (nums.size () < 2) return nums.size (); int len = 1; for (int i = 1; i < nums.size (); i++) { if (nums[i]! = nums[i-1]) nums[len++] = nums[i]; } return len; };
Note: This blog post is Ebowtang original and may continue to be updated later in this article. If reproduced, please make sure to copy this article information!
Original address: http://blog.csdn.net/ebowtang/article/details/50499722
Original Author Blog: Http://blog.csdn.net/ebowtang
<leetcode oj> 26. Remove Duplicates from Sorted Array