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. Eclipse rap 富客户端开发总结(9) : rap上传与下载

    一 上传 上传即将文件上传到服务器上,在客户端需要写相应的脚本,服务器端需要注册相应的 handle 接受客户端的请求. 原理: Rap 的上传和下载是通过普通的 web 的方式进行上传和下载的 ,  ...

  2. java.lang.IllegalArgumentException: node to traverse cannot be null!

    查看HQL的语句是否写错了,是否有在From后面加空格.我就是没有加空格报了错误! return sessionFactory.getCurrentSession().createQuery(&quo ...

  3. linux free命令解读

    $ free -m total used free shared buffers cached Mem: 1002 769 232 0 62 421 -/+ buffers/cache: 286 71 ...

  4. 记2017问鼎杯预赛的wp---来自一个小菜鸡的感想

    这次准备写一下几个misc和密码题目..很坑. 打了一整天的比赛,越来越觉得自己很菜了. 有一道题目叫做"真真假假",这道题目只有一个提示--Xor.第一眼知道是异或,也就知道这一 ...

  5. PHP浮点型(float)转换为整形(int)/round()保留小数点后几位

    round(x,y); x:需要转换的变量 y:保留几位小数 <?php echo round(3.112312321) //输出3 echo round(3.112312321,3) //输出 ...

  6. 30分钟快速学习Shell脚本编程

    什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_tut for ((i=0; i<10; i++)); do touch ...

  7. IDE快捷键

    visual studio 中 : /// 快速函数注释 ctrl+k,ctrl+F 格式化代码 Android Studio  中: /**回车 快速函数注释 ctrl+h 查看继承关系 自动格式化 ...

  8. File FileStream StreamWriter StreamReader文件读写操作方法

    string path = "D:\\AccountChecking\\Test.txt"; string content = "abcdefg\r\nhigklmn\r ...

  9. FPGA在其他领域的应用(一)

    测试和测量应用: 测试需要是所有细分市场的要求.无论是终端市场,所有产品在运到最终客户之前都必须进行测试.这动态地驱动测试和测量领域的普遍性质,其中包括下面的种类和分段: 通信测试: 无线测试仪 (W ...

  10. jdbc-批处理

     批处理 1 Statement批处理 批处理就是一批一批的处理,而不是一个一个的处理! 当你有10条SQL语句要执行时,一次向服务器发送一条SQL语句,这么做效率上很差!处理的方案是使用批处理, ...