LeetCode-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].

 

源代码:

 1 int removeDuplicates(int A[], int n) {
 2       if(n==0) return 0;
 3           
 4       int start=0;int key=A[0];
 5       for(int i=0;i<n;i++){
 6             if(A[i]!=key){
 7                  A[start]=key;
 8                  key=A[i];
 9             }
10       }
11       
12       A[start++]=key;
13       return start;
14 }

 

posted @ 2014-10-10 17:30  晓风_7  阅读(104)  评论(0编辑  收藏  举报