26. Remove Duplicates from Sorted Array[E]删除排序数组中的重复项
题目
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]删除排序数组中的重复项的更多相关文章
- 26. Remove Duplicates from Sorted Array C++ 删除排序数组中的重复项
https://leetcode.com/problems/remove-duplicates-from-sorted-array/ 双指针,注意初始时左右指针指向首元素! class Solutio ...
- **80. Remove Duplicates from Sorted Array II 删除排序数组中的重复项 II
1. 原始题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件 ...
- [LC]26题 Remove Duplicates from Sorted Array (删除排序数组中的重复项)(双指针法)(原地实现)
①中文题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...
- Leetcode80. Remove Duplicates from Sorted Array II删除排序数组中的重复项2
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 ...
- lintcode :Remove Duplicates from Sorted Array II 删除排序数组中的重复数字 II
题目: 删除排序数组中的重复数字 II 跟进“删除重复数字”: 如果可以允许出现两次重复将如何处理? 样例 给出数组A =[1,1,1,2,2,3],你的函数应该返回长度5,此时A=[1,1,2,2, ...
- Leetcode82. Remove Duplicates from Sorted List II删除排序链表中的重复元素2
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 示例 1: 输入: 1->2->3->3->4->4->5 输出: 1-&g ...
- [LC]83题 Remove Duplicates from Sorted List(删除排序链表中的重复元素)(链表)
①英文题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exa ...
- 力扣—Remove Duplicates from Sorted List(删除排序链表中的重复元素)python实现
题目描述: 中文: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2输出: 1->2 示例 2: 输入: 1->1->2 ...
- LeetCode(26): 删除排序数组中的重复项
Easy! 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间 ...
随机推荐
- Tinyxml2学习
转自http://www.360doc.com/content/13/1223/16/3684846_339528825.shtml,尊重原文 什么是XML? XML全称EXtensible Mark ...
- 【sqli-labs】 less26 GET- Error based -All you SPACES and COMMENTS belong to us(GET型基于错误的去除了空格和注释的注入)
看了下源码 所有的注释形式和反斜线,and,or都被了过滤掉了 单引号没有过滤 空格也被过滤了 http://localhost/sqli-labs-master/Less-26/?id=1' htt ...
- 【sqli-labs】 less21 Cookie Injection- Error Based- complex - string ( 基于错误的复杂的字符型Cookie注入)
这个和less20是一样的,唯一的不同在于添加了括号和使用了base64对cookie进行了编码(因为使用了base64_decode解码函数) admin被编码成了YWRtaW4=但是执行的SQL语 ...
- c# 验证码实现代码
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D ...
- tomcat 热加载设置
找到tomcat项目的apache-tomcat-8.0.30\conf\context.xml,打开进行编辑,把Context项中加上 reloadable="true" < ...
- 【第二课】kaggle案例分析二
Evernote Export 推荐系统比赛(常见比赛) 推荐系统分类 最能变现的机器学习应用 基于应用领域分类:电子商务推荐,社交好友推荐,搜索引擎推荐,信息内容推荐等 **基于设计思想:**基于协 ...
- How to use pthread_create && mutex?
1 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread. ...
- Java实验环境搭建
1.JDK的下载一.JDK的下载及安装 (1).网站网址搜索http://www.oracle.com/technetwork/java,进入浏览页(2)找到Trials and Download 点 ...
- DOM学习之图片库切换效果
addloadevent(prepareplaceholder()) addloadevent(prepareGallery()) //页面加载完时执行函数 function addloadevent ...
- Codeforces 919D Substring (拓扑图DP)
手动博客搬家: 本文发表于20180716 10:53:12, 原地址https://blog.csdn.net/suncongbo/article/details/81061500 给定一个\(n\ ...