Sunday, May 10, 2015

Remove Duplicates from Sorted Array I

来源:Leetcode

原帖:http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/

题目:
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array A = [1,1,2], Your function should return length = 2, and A is now [1,2].

代码:
 class Solution {  
 public:  
   int removeDuplicates(int A[], int n) {  
     int start = 0;  
     for (int i = 0; i < n; ++i) {  
       if (i == 0 || A[i] != A[start - 1]) {  
         A[start++] = A[i];                    
       }  
     }  
     return start;  
   }  
 };  

No comments:

Post a Comment