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 Solution {

public int removeDuplicates(int[] A) {

    int j=1;//快慢指针,快指针寻到不同的元素时,慢指针才动

    if(A==null)return 0;

    if(A.length<=1)return A.length;

    for( int i=1;i<A.length;++i){    


    if(A[i]!=A[i-1]){

    A[j++]=A[i];

    }

    }   

    return j;        

    }

   

}

RemoveDuplicatesfromSortedArray的更多相关文章

  1. leetcode — remove-duplicates-from-sorted-array

    import java.util.Arrays; /** * Source : https://oj.leetcode.com/problems/remove-duplicates-from-sort ...

  2. Array - RemoveDuplicatesfromSortedArray

    /** * 无额外空间,只要前n个是不重复的就行,不需要修改后面的数字 * @param nums 已排序的数组 * @return 去除重复数字后的长度 */ public int removeDu ...

  3. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  4. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  5. No.026:Remove Duplicates from Sorted Array

    问题: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  6. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  7. 转载 ACM训练计划

    leetcode代码 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode. ...

  8. LeetCode题目分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  9. <转>LeetCode 题目总结/分类

    原链接:http://blog.csdn.net/yangliuy/article/details/44514495 注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http:/ ...

随机推荐

  1. Linux 常用命令速查

    0x001 .在指定文件夹下递归查询包含一个字符串的文件(列出的文件内容片段) grep -r   “要查找的串”    文件路径   如  : grep -r  "helloworld&q ...

  2. 安卓开发之玩美解决ADT和SDK不一致问题

    提示:This Android SDK requires Android Developer Toolkit version 21.1.0 or above.  Current version is ...

  3. atitit.atiLinq v2新特性attilax大总结 q326

    atitit.atiLinq v2新特性attilax大总结 q326 1. V3规划 (分开sql2obj sql2sql sql2xml)1 2. V2新特性 Url linq的定义1 3. V1 ...

  4. LNMP平滑升级nginx并安装ngx_lua模块教程

    #ngx_lua module项目地址 https://github.com/chaoslawful/lua-nginx-module 在LNMP安装包后,重编译nginx,并添加ngx_lua模块 ...

  5. ssh加密访问

    ssh 加密访问    telnet 开放访问需安装软件openssh-server    $ssh akaedu@192.168.103.114    $ssh 192.168.103.114 附: ...

  6. Servlet 自动刷新页面

    假设有一个网页,它是显示现场比赛成绩或股票市场状况或货币兑换率.对于所有这些类型的页面,您需要定期刷新网页. Java Servlet 提供了一个机制,使得网页会在给定的时间间隔自动刷新. 刷新网页的 ...

  7. 消息队列(message queue)

    最近纠结于一个问题,就是horizon 在处理前台数据的时候非得等到cinder client将数据全部获取后才开始执行horizon的下一行代码,这给大量数据显示造成了很大的时延,其实对于用户体验来 ...

  8. Android-ViewPagerIndicator框架使用——TabPageIndicator

    前言:TabPageIndicator这个类和之前的不大一样,他不仅提供了展示的功能,而且可以点击,下面的viewpager可以跳转的有点tabhost的感觉. 一:布局文件的定义,simple_ta ...

  9. C++11写算法之顺序查找

    从这篇博文起,将尝试使用C++11来写常用算法与数据结构. 本篇博文以最简单的顺序查找作为系列博文的起点,并作约定如下: 1,变量名 : varList : 函数名 : SequentialFind ...

  10. Linux文件类型及目录配置

    Linux文件类型与扩展名 在Linux系统中,任何硬件设备或者其他设备都是以文件的形式存在,就连数据通信的接口这些也是由专门的文件来负责的,因此Linux的文件种类就非常多,出了之前我们常见的 - ...