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

[解题思路]

双指针问题,count指针表示结果数组的大小

 1 public class Solution {
 2     public int removeDuplicates(int[] A) {
 3         int len = A.length;
 4         if(len == 0){
 5             return 0;
 6         }
 7         int count = 1;
 8         
 9         for(int i = 1; i < len; i++){
10             if(A[i] == A[i-1]){
11                 
12                 continue;
13             }
14             A[count ++] = A[i];
15         }
16         
17         return count;
18     }
19 }

 

posted @ 2013-08-30 12:57  feiling  阅读(159)  评论(0编辑  收藏  举报