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. 工作总结 Ajax.BeginForm 默认action

    生成源代码 点提交 总结 BeginForm 的 active 默认指向 进当前页面的 操作方法    若第一次进页面时带着参数, 那么也会将参数放到active 简单点  BeginForm 的 a ...

  2. 查看、修改linux系统的最大链接数限制、文件描述符限制、端口范围限制、虚拟内存等

    一.修改最大连接数 1.查看当前文件描述符的限制数目的命令: ulimit -n 2.修改文件描述符的限制数目 2.1 临时改变当前会话: ulimit -n 65536 2.2 永久变更需要下面两个 ...

  3. POJ 3561 Pseudographical recognizer

    [题意简述]:矩阵中除了'.'仅仅能出现一种符号.是这些之中的一个'‑', '|', '\', or '/',并且就是当除了'.'之外还仅仅有一种符号时.这个符号还必须连成一条直线,否则就是错的,这个 ...

  4. 知也atitit.解决struts2 SpringObjectFactory.getClassInstance NullPointerException  v2 q31无涯 - I

    atitit.解决struts2 SpringObjectFactory.getClassInstance NullPointerExceptionv2 q31 1. #--现象 java.lang. ...

  5. PILE读书笔记_标准I/O

    在学习和分析标准I/O库的同时, 可以重点与Linux的I/O系统调用进行比较. stdin. stdout和stderr都是FILE类型的文件指针, 是由C库静态定义的, 直接与文件描述符0. 1和 ...

  6. shell脚本之微信报警功能的实现

    导语:现在越来越流行微信报警功能了.下面就来看看具体实现吧! 1.先申请一个微信企业号 传送门:http://work.weixin.qq.com/ 2.添加用户 2.创建应用 3.创建管理组并添加管 ...

  7. java 无状态和有状态区别

     诸位Java程序员,想必大家对SimpleDateFormat并不陌生.不过,你是否知道,SimpleDateFormat不是线程安全的(thread safe).这意味着,下面的代码是错误的: ...

  8. 【转】windows下python开发环境搭建

    1 -- 安装python的前期准备 Python开发有众多工具,又以Eclipse+Pydev最为常见.Eclipse平台对开发同学来讲,肯定是如雷贯耳,自不用废话.而PyDev是Eclipse平台 ...

  9. Mysql事务-隔离级别

    MYSQL事务-隔离级别 事务是什么? 事务简言之就是一组SQL执行要么全部成功,要么全部失败.MYSQL的事务在存储引擎层实现. 事务都有ACID特性: 原子性(Atomicity):一个事务必须被 ...

  10. ASP.NET动态网站制作(27)-- 三层框架(1)

    前言:今天主要介绍一下三层框架,给大家一个整体的概念.分层概念使得程序低耦合,更加健壮,扩展性更好. 内容: 1.三层: UI(表现层):主要是指与用户交互的界面.用于接收用户输入的数据和显示处理后用 ...