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. Go环境IDE安装配置

    终于配好了自己的Go环境,每天可以来一点积累了. MAC安装配置过程参考了如下几个博文~谢谢 Intellij安装配置: http://blog.csdn.net/fenglailea/article ...

  2. 转: android emulator 命令详解

    在命令行输入: emulator -help,即可显示emulator支持的所有命令. Android Emulator usage: emulator [options] [-qemu args] ...

  3. Atitit.atiDataStoreService   v2 新特性

    Atitit.atiDataStoreService   v2 新特性 1.1. V1  基础实现1 1.2. V2  增加了对  $uuid  $cur_uid参数的支持1 1.3. 增加了fld  ...

  4. 如何将webbrowser控件的Cookie倒入CookieContainer供WebRequest使用

    先建一个 "CookieContainer "   把WebBrowser中的Cookie保存在里面                       //在WebBrowser中登录 ...

  5. python简单C/S模式示例

    服务器端代码: #!/usr/bin/python import time, socket, threading # thread handle function def tcplink(sock, ...

  6. Jenkins安装与构建部署

    Jenkins是一个开源软件项目,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能.Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括:1.持续的软件版本发布 ...

  7. Help Tomisu UVA - 11440 难推导+欧拉函数,给定正整数N和M, 统计2和N!之间有多少个整数x满足,x的所有素因子都大于M (2<=N<=1e7, 1<=M<=N, N-M<=1E5) 输出答案除以1e8+7的余数。

    /** 题目:Help Tomisu UVA - 11440 链接:https://vjudge.net/problem/UVA-11440 题意:给定正整数N和M, 统计2和N!之间有多少个整数x满 ...

  8. 字符串截取mb_substr

    mb_substr("字符串","截取开始位置","截取个数","编码格式如UTF-8")

  9. Google Careers 程序员必修课

    quote from : https://www.google.com/about/careers/students/guide-to-technical-development.html Techn ...

  10. 在iframe的父级作用域操作,ifame中的元素。。

    frames["iframe的name"].SchDatas SchDatas为方法名js中 frames["iframe的name"].document.ge ...