Question

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 nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.

Solution -- Two Pointers

Time complexity O(n), space cost O(1)

 public class Solution {
public int removeDuplicates(int[] nums) {
int length = nums.length;
if (length <= 1)
return length;
int p1 = 0, p2 = 1;
while (p2 < length) {
if (nums[p2] != nums[p1]) {
p1++;
nums[p1 + 1] = nums[p2];
p2++;
} else {
p2++;
}
}
return p1 + 1;
}
}

Remove Duplicates from Sorted Array 解答的更多相关文章

  1. LeetCode Array Easy 26.Remove Duplicates from Sorted Array 解答及疑惑

    Description Given a sorted array nums, remove the duplicates in-place such that each element appear ...

  2. leetCode练题——26. Remove Duplicates from Sorted Array

    1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates  ...

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

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

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

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

  5. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  6. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  7. 26. Remove Duplicates from Sorted Array

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

  8. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  9. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

随机推荐

  1. c++ 08

    一.程序的错误 1.编码错误:编译阶段 2.设计错误:测试阶段 3.环境错误:使用阶段 4.应用错误:测试和使用阶段 二.错误处理机制 1.通过返回值处理错误 当一个函数在执行过程中发生了某种错误,通 ...

  2. 虚拟机环境中安装ubuntu下的mysql-cluster7.3.2(单点服务器)

      部署环境: 系统:ubuntu-12.04.2 LTS -server-i386.iso Cluster:mysql-cluster-gpl-7.3.2-linux-glibc23-i686.ta ...

  3. vi命令笔记

    vim编辑器 文本编辑器,字处理器ASCII nano, sed vi: Visual Interfacevim: VI iMproved 全屏编辑器,模式化编辑器 vim模式:编辑模式(命令模式)输 ...

  4. IO队列和IO调度

    IO体系概览 先看看本文主题IO调度和IO队列处于整个IO体系的哪个位置,这个IO体系是非常重要的,了解IO体系我们可以对整个IO过程有个全面的认识.虽然一下两下并不清楚IO体系各个部分的细节,但是我 ...

  5. Android学习笔记__1__Android体系架构

    Android 体系结构图 Android作为一个移动设备的平台,其软件层次结构包括了一个操作系统(OS),中间件(MiddleWare)和应用程序(Application).根据Android的软件 ...

  6. java与.net比较学习系列(7) 属性

    文章摘自:http://www.cnblogs.com/mcgrady/p/3411405.html 说起属性,实际上java中没有属性这个概念,只有字段和方法,但是可以通过私有字段和声明get,se ...

  7. Android系统匿名共享内存Ashmem(Anonymous Shared Memory)驱动程序源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6664554 在上一文章Android系统匿名共 ...

  8. asp.net MVC 学习笔记

    1.可以看出每个区域Areas里都是个mini的MVC项目,Controller.Models.Views一个都不缺,还多了一个AdminAreaRegistration类 2.MVC 将URL映射到 ...

  9. Windows - 程序猿应该熟记的CMD常用命令

    notepad 计事本 mspaint 画图 iisreset 重启IIS appwiz.cpl 控制面板 inetmgr IIS管理器 eventvwr 事件查看器 mstsc 远程桌面 net s ...

  10. RDLC添加链接

    <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdan ...