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 public class Solution {
 2     public int removeDuplicates(int[] A) {
 3         if (A == null||A.length ==0 || A.length==1) {
 4             return A.length;
 5         }
 6         int slow = 0;
 7         for (int i = 1; i < A.length; i++) {
 8             if (A[i]==A[slow]) {
 9 
10                 continue;
11             }else {
12                 A[++slow]=A[i];
13             }
14             
15         }
16         return slow+1;
17         
18     }
19 }
复制代码

 

posted @   birdhack  阅读(140)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示