题目


Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.

Example 1:

 Given 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 returned length.

Example 2:

 Given nums = [0,0,1,1,1,2,2,3,3,4],

 Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

 It doesn't matter what values are set beyond the returned length.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well. Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);
// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}

思路


双指针法

本题要返回一个有序数组内不重复元素的个数,要比较数组内两个元素是否相同,需要两个下标记录,于是想到双指针法。这里要注意两点:一是函数的形参是传引用,改变形参也会改变原本的数据;二是题目要求不得声明额外的空间。

对于一个有序数组,由于不能使用额外的空间保存删除重复元素后的数组,于是想到不删除重复元素,而是替换重复元素,将重复元素替换到后面,使用两个指针:

  • pBegin:记录不重复数组的最后一个元素的下标。

    • 遇到不重复元素时,pBegin加1,表示多了一个不重复元素。
  • pEnd:用于循环。
    • 当遇到重复元素时,pEnd加1
    • 当遇到不重复元素时,先pBegin加1,此时pBegin指向的还是重复元素,pEnd指向不重复元素。将pBegin所指元素与pEnd所指元素替换,此时pBegin指向不重复元素,并且下标代表不重复数组的最后一个下标;pEnd指向重复元素。

C++

class Solution {
public:
int removeDuplicates(vector<int>& nums) { if(nums.size() == 0)
return 0; int pBegin = 0;
int pEnd = 1; while(pEnd < nums.size()){
if(nums[pEnd] != nums[pBegin]){
pBegin ++;
nums[pBegin] = nums[pEnd];
}
pEnd ++;
}
return pBegin + 1; //从0计数的,因此长度+1
}
};

Python

26. Remove Duplicates from Sorted Array[E]删除排序数组中的重复项的更多相关文章

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

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

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

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

  3. [LC]26题 Remove Duplicates from Sorted Array (删除排序数组中的重复项)(双指针法)(原地实现)

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

  4. Leetcode80. Remove Duplicates from Sorted Array II删除排序数组中的重复项2

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

  5. lintcode :Remove Duplicates from Sorted Array II 删除排序数组中的重复数字 II

    题目: 删除排序数组中的重复数字 II 跟进“删除重复数字”: 如果可以允许出现两次重复将如何处理? 样例 给出数组A =[1,1,1,2,2,3],你的函数应该返回长度5,此时A=[1,1,2,2, ...

  6. Leetcode82. Remove Duplicates from Sorted List II删除排序链表中的重复元素2

    给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 示例 1: 输入: 1->2->3->3->4->4->5 输出: 1-&g ...

  7. [LC]83题 Remove Duplicates from Sorted List(删除排序链表中的重复元素)(链表)

    ①英文题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exa ...

  8. 力扣—Remove Duplicates from Sorted List(删除排序链表中的重复元素)python实现

    题目描述: 中文: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2输出: 1->2 示例 2: 输入: 1->1->2 ...

  9. LeetCode(26): 删除排序数组中的重复项

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

随机推荐

  1. Python+selenium第一个自动化脚本

    第一个自动化脚本(用Python写的) from selenium import webdriver  #从selenium导入webdriber driver=webdriber.Firefox() ...

  2. 点云处理软件Pointscene

    转载于PCL中国:点云处理软件Pointscene 软件官网:https://pointscene.com/ 笔者评:        Pointscene是目前的点云处理软件之一,其主要是操作简单直观 ...

  3. [实战经验][SQL Sever 2008 (R)解决方法累积

    SQL Sever 2008 (R)的安装图解及配置 http://www.soft6.com/v9/2009/jcsj_1030/115821.html 产品密钥,选择“输入产品密钥”,输入:PTT ...

  4. jquery Contains 实现查询

    var filter = $(this).val(); var filterResult = $(this).find('h2:Contains(' + filter + ')'); if (filt ...

  5. google浏览器 打印A4 最大宽度和高度px

    width: 1563px;(max) + = 分页了 + = 分页了 + = 没有分页 / ViewBag.results[].Count)); <td width="15%&quo ...

  6. java rsa加密解密

  7. .NET 人工智能相关资料整理

    机器学习组件:https://www.cnblogs.com/asxinyu/p/dotnet_Opensource_project_AccordNET.html ML.NET:            ...

  8. 微信小程序 PDF下载打印

    在开发微信小程序时,需要打印生成的PDF,实现思路是:后端生成相应的PDF,微信小程序下载并打开. 但是微信小程序并不可以打印,所以需要借助其他APP比如:WPS,但是发现微信小程序down的PDF在 ...

  9. 谨慎调整内核参数:vm.min_free_kbytes

    内核参数:内存相关 内存管理从三个层次管理内存,分别是node, zone ,page; 64位的x86物理机内存从高地址到低地址分为: Normal DMA32 DMA.随着地址降低. [root@ ...

  10. X5内核浏览器,video兼容

    使用vue-video-player在移动端微信内置浏览器打开,点击视频自动全屏问题. 参考官方 API 是 H5 同层浏览器的原因,可通过设置video属性来处理. <video-player ...