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) 额外空间 ...
随机推荐
- 推荐系统入门:作为Rank系统的推荐系统(协同过滤)
知乎:如何学习推荐系统? 知乎:协同过滤和基于内容的推荐有什么区别? 案例:推荐系统实战? 数据准备:实现推荐栏位:重构接口:后续优化. 简书:实现实时推荐系统的三种方式?基于聚类和协同过滤:基于S ...
- jsp+servlet 导出Excel表格
1.项目的目录结构 2.创建一个用户类,下面会通过查询数据库把数据封装成用户实例列表 package csh.entity; /** * @author 悦文 * @create 2018-10-24 ...
- [jzoj 5782]【NOIP提高A组模拟2018.8.8】 城市猎人 (并查集按秩合并+复杂度分析)
传送门 Description 有n个城市,标号为1到n,修建道路花费m天,第i天时,若gcd(a,b)=m-i+1,则标号为a的城市和标号为b的城市会建好一条直接相连的道路,有多次询问,每次询问某两 ...
- SVN仓库导入文件
分两种: 1.导入文件版本库从0开始 (适合新项目) 2.将其他SVN服务器中的版本库导入进来,版本库继承原SVN服务器的(适合SVN版本库迁移) 第一种: #mkdir –p /home/code/ ...
- linux邮件服务
linux本地常见邮件服务有: Centos5:默认使用sendmail邮件服务,开启方式/etc/init.d/sedmail start Centos6:默认使用postfix邮件服务,开启方式/ ...
- django QuerySet对象转换成字典对象
>>> from django.contrib.auth.models import User >>> from django.forms.models impor ...
- vscode简单使用介绍及个人常用扩展插件
vscode全称Visual Studio Code 是微软开发一款IDE,官方地址 vscode 作为一款前端编辑器功能很强大,灵活,可以根据个人喜好选择扩展插件,而且还支持多种开发语言, 关于v ...
- 洛谷 P3183 BZOJ 4562 [HAOI2016]食物链
题目描述 如图所示为某生态系统的食物网示意图,据图回答第1小题现在给你n个物种和m条能量流动关系,求其中的食物链条数.物种的名称为从1到n编号M条能量流动关系形如a1 b1a2 b2a3 b3.... ...
- SCU - 4117 - Discover
先上题目: D - Discover Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit St ...
- [bzoj3389][Usaco2004Dec]Cleaning Shifts安排值班_最短路
Cleaning Shifts bzoj-3389 Usaco-2004Dec 题目大意:每天有n个时间段,每个时间段都必须安排一个奶牛值班.有m个奶牛,每个奶牛只有一个空闲时间s[i]~e[i],求 ...