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.


题目标签:Array
  这道题目给了我们一个有序array, 让我们把重复的都移除,并且要求我们不能利用额外的空间,只能在原有的array里改。这里需要两个pointers, i 和 j。 设置j 为0, i 从1 开始遍历。目的是让i 找到一个不重复的数字,一旦找到,我们把这个数字放在j 的后面一个位子。这样的话,所有重复的数字,都会放在j后面。j之前的包括j的部分都是不重复的。最后只要return j+1 就可以了。
 
 

Java Solution:

Runtime beats 55.60%

完成日期:03/09/2017

关键词:Array

关键点:利用two pointers

 public class Solution
{
public int removeDuplicates(int[] nums)
{
// check validation.
if(nums.length == 0)
return 0; int j = 0; // iterate nums array.
for(int i=1; i<nums.length; i++)
{
// once find the nums[i] that doesn't match nums[j]
if(nums[i] != nums[j])
{
j++;
// swap the non-duplicate one with duplicate one or with itself
nums[j] = nums[i];
} } return j+1;
}
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 26. Remove Duplicates from Sorted Array (从有序序列里移除重复项)的更多相关文章

  1. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

  2. 26. Remove Duplicates from Sorted Array[E]删除排序数组中的重复项

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

  3. LeetCode#26 | Remove Duplicates from Sorted Array 删除有序数组中的重复元素

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

  4. 26. Remove Duplicates from Sorted Array C++ 删除排序数组中的重复项

    https://leetcode.com/problems/remove-duplicates-from-sorted-array/ 双指针,注意初始时左右指针指向首元素! class Solutio ...

  5. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

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

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

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

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  8. **80. Remove Duplicates from Sorted Array II 删除排序数组中的重复项 II

    1. 原始题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件 ...

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

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

  10. leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法

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

随机推荐

  1. openfire:Openfire源代码在eclipse中的运行配置 + 与spark结合进行二次开发

    1.下载源代码:http://www.igniterealtime.org/downloads/source.jsp 2.把源代码解压出的openfire_src文件夹放至eclipse workpl ...

  2. CustomDialog——一个多功能、通用、可自定义的Dialog

    CustomDialog--一个多功能.通用.可自定义的Dialog  依赖 compile 'com.github.SiberiaDante:CustomDialog:v1.0.1'  说明[Cus ...

  3. [04] Cookie概念和基本使用

    1.Cookie是什么 Cookie,中文名称为"小型文本文件"或"小甜饼",指某些网站为了辨别用户身份而储存在用户本地终端上的数据(通常经过加密). 很多网站 ...

  4. Hibernate映射乱码

    1.修改数据库字符集:将数据库默认编码设置为UTF-8 CHARSET=utf8 2.配置Hibernate环境时将数据库URL设置成: jdbc:mysql://localhost:3306/dbN ...

  5. birt-j脚本调试 & 动态sql的实现

    一个比较好的birt问题解决网址: http://www.myexception.cn/h/1335919.html 1,Birt的JavaScript脚本简单调试 Birt中的js脚本不能用aler ...

  6. vue源码学习-vnode的挂载和更新流程

    概述 本文主要介绍在视图的渲染过程中,Vue 是如何把 vnode 解析并挂载到页面中的.我们通过一个最简单的例子来分析主要流程: <div id="app"> {{s ...

  7. java踩坑记

    1.String 相等 稍微有点经验的程序员都会用equals比较而不是用 ==,但用equals就真的安全了吗,看下面的代码 user.getName().equals("xiaoming ...

  8. Scaffolding Template on Asp.Net Core Razor Page

    Scaffolding Template Intro 我们知道在Asp.Net MVC中,如果你使用的EF的DBContext的话,你可以在vs中通过右键解决方案-添加控制器-添加包含视图的控制器,然 ...

  9. Highway Networks Pytorch

    导读 本文讨论了深层神经网络训练困难的原因以及如何使用Highway Networks去解决深层神经网络训练的困难,并且在pytorch上实现了Highway Networks. 一 .Highway ...

  10. 一个基于Asp.net MVC的博客类网站开源了!

    背景说明: 大学时毕业设计作品,一直闲置在硬盘了,倒想着不如开源出来,也许会对一些人有帮助呢,而且个人觉得这个网站做得还是不错了,毕竟是花了不少心思,希望对你有所帮助. github地址:https: ...