Leetcode: Remove Duplicates from Sorted Array

 1 class Solution {
 2 public:
 3 int removeDuplicates(int arr[], int n) {
 4     // Start typing your C/C++ solution below
 5     // DO NOT write int main() function
 6 if(n<=1)
 7     return n;
 8 int insertPos = 0; 
 9 for (int i = 1; i < n; i++)
10 {
11     if (arr[i] != arr[insertPos])
12         arr[++insertPos] = arr[i];
13 }
14 return insertPos+1;
15 }
16 };

 

posted @ 2013-05-01 21:23  caijinlong  阅读(101)  评论(0编辑  收藏  举报