Chinaunix首页 | 论坛 | 博客
  • 博客访问: 254353
  • 博文数量: 170
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1709
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 18:01
文章分类

全部博文(170)

文章存档

2016年(11)

2015年(130)

2014年(29)

分类: Java

2015-04-09 11:21:42

//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].
public class RemoveDuplicatesfromSortedArray {


public static void main(String[] args) {
// TODO 自动生成的方法存根


}
public int removeDuplicates(int[] A) {
if(A.length==0||A.length==1)
return A.length;
int i=1;
int pre=A[0];
int j=1;
        for(;i         if(pre!=A[i]){
        pre=A[i];
        A[j]=pre;
        j++;
        }
       
        }
        return j;
    }



}

阅读(417) | 评论(0) | 转发(0) |
0

上一篇:Reverse Nodes in k-Group

下一篇:Remove Element

给主人留下些什么吧!~~