有序数组中删除重复元素

本文提供多种方法解决删除已排序数组中的重复元素问题,包括保证每个元素只出现一次及最多出现两次的情况。通过不同类别的解决方案展示算法实现细节,并附带主函数用于验证算法效果。

问题1:删除已排序的数组中重复元素,保证每个元素只出现1次。

问题2:删除已排序数组中的重复元素,每个元素最多只出现2次。



技术参考: 

upper_bound and lower bound: https://blog.youkuaiyun.com/xiaohei00000/article/details/51010292

distance and advance: http://www.cnblogs.com/youxin/archive/2012/06/16/2552262.html

unique and unique_copy: http://www.cnblogs.com/heyonggang/p/3243477.html

typename and class: http://feihu.me/blog/2014/the-origin-and-usage-of-typename/

upper_bound: 返回大于某个元素的最小位置, 类型为 指针

lower_bound: 返回大于等于某个元素的最小位置, 类型为 指针

distance: 返回两个位置之间的距离,类型为 整型

unique:(使用前需 #include <algorithm> )容器(含数组)内的元素个数不变,最前一段不含相邻重复元素(即使用前应先排序),重复元素全部集中到后面的部分,返回 最前一段的下一位置,类型为 指针

unique_copy: 同unique, 将不重复的元素copy到另一容器中。

list排序:list.sort();

vector排序: sort(vector.begin(),vector.end());


问题1:删除已排序的数组中重复元素,保证每个元素只出现1次。

输入: A=[1,1,2],

输出: lenth=2, A=[1,2]

代码参考:(Solution 1-3 拷贝自 LeetCode 题解 ,Solution 4-5 源自 问题2 的解法改编)

需包含的库文件:

#include <iostream>
#include <algorithm>
using namespace std;
class Solution1{
	public:
		int removeDuplicates(int A[], int n){
			int index=0;
			for(int i=1;i<n;++i){
				if(A[index]!=A[i])
					A[++index]=A[i];
			}
			return index+1;
		}
};
class Solution2{
	public:
		int removeDuplicates(int A[], int n){
			return distance(A,unique(A,A+n));
		}
};
class Solution3{
	public:
		int removeDuplicates(int A[], int n){
			return removeDuplicates(A,A+n,A)-A;
		}
		template<typename InIt, typename OutIt>
		OutIt removeDuplicates(InIt first, InIt last, OutIt output){
			while(first != last){
				*output++ = *first;
				first=upper_bound(first,last,*first);
			}
			return output;
		}
};
class Solution4{
	public:
		int removeDuplicates(int A[], int n){
			int index=0;
			for(int i=0;i<n;++i){
				if(i>0 && i<n && A[i-1] == A[i])
					continue;
				else
					A[index++]=A[i];
			}
			return index;
		}
};
class Solution5{
	public:
		int removeDuplicates(int A[], int n){
			if (n<2) return n;
			int index=1;
			for(int i=1;i<n;i++){
				if(A[i] != A[index -1]){
					A[index++] = A[i];
				}
			}
			return index;
		}
};

主函数:

int main()
{
	int n;
	int A[100];
	cout <<"input n:";
	cin >>n;
	cout <<"input A:";
	for(int i=0;i<n;++i)
		cin >>A[i];
	Solution sol;
	
	n=sol.removeDuplicates(A,n);
	cout <<"new array A is:" <<endl;
	for(int i=0;i<n;++i){
		cout <<A[i] <<" ";
	}
	cout <<endl;
	cout <<"length is:" <<n;
	cout <<endl;

	return 0;	
}

问题2:类似问题1,删除已排序数组中的重复元素,每个元素最多只出现2次。Solution 1-2 拷贝自 LeetCode 题解,Solution 1作者:hex108 , Solution 2 作者: 虞航仲


class Solution1{
	public:
		int removeDuplicates(int A[], int n){
			if (n<=2) return n;
			int index=2;
			for(int i=2;i<n;i++){
				if(A[i] != A[index -2]){
					A[index++] = A[i];
				}
			}
			return index;
		}
};
class Solution2{
	public:
		int removeDuplicates(int A[], int n){
			int index=0;
			for(int i=0;i<n;i++){
				if(i>0 && i<n-1 && A[i] == A[i-1] && A[i] == A[i+1])
					continue;
				A[index++]=A[i];
			}
			return index;
		}
};


技术参考: 

upper_bound and lower bound: https://blog.youkuaiyun.com/xiaohei00000/article/details/51010292

distance and advance: http://www.cnblogs.com/youxin/archive/2012/06/16/2552262.html

unique and unique_copy: http://www.cnblogs.com/heyonggang/p/3243477.html

typename and class: http://feihu.me/blog/2014/the-origin-and-usage-of-typename/

upper_bound: 返回大于某个元素的最小位置, 类型为 指针

lower_bound: 返回大于等于某个元素的最小位置, 类型为 指针

distance: 返回两个位置之间的距离,类型为 整型

unique:(使用前需 #include <algorithm> )容器(含数组)内的元素个数不变,最前一段不含相邻重复元素(即使用前应先排序),重复元素全部集中到后面的部分,返回 最前一段的下一位置,类型为 指针

unique_copy: 同unique, 将不重复的元素copy到另一容器中。

list排序:list.sort();

vector排序: sort(vector.begin(),vector.end());

<think>我们正在讨论“删除有序数组中的重复项”实验的目的。根据引用内容,这个实验通常出现在算法题目中(如LeetCode),涉及双指针技巧、数组操作和边界控制。实验要求原地修改数组,并返回新长度,同时保持元素的相对顺序一致。 实验目的可以从以下几个方面理解: 1. **算法训练**:掌握双指针技巧(快慢指针),这是解决数组/链表问题的要方法。 2. **数组操作**:练习原地修改数组,提高空间复杂度为$O(1)$的处理能力。 3. **边界控制**:注意数组边界(如空数组、单元素数组)和重复元素连续出现时的处理。 4. **问题扩展**:基础题目(如每个元素只出现一次)的扩展(如允许元素最多出现两次)。 此外,引用提到这个实验为开发高性能功能提供基础,说明其实际应用价值。 因此,删除有序数组中的重复项实验的主要目的包括: - 训练双指针技巧 - 优化空间复杂度(原地操作) - 培养边界条件处理能力 - 为实际问题(如数据)提供解决方案 §§相关问题§§ 1. 双指针技巧在删除数组重复项中如何具体应用? 2. 原地删除数组重复项的空间复杂度优势体现在哪里? 3. 如何修改算法使有序数组中每个元素最多保留两次? 4. 删除重复项后,为什么需要返回新长度而非新数组? </think>### 删除有序数组中的重复项实验的目的 删除有序数组中的重复项是算法训练中的经典实验,其主要目的包含以下核心层面: #### 1. **掌握核心算法技巧** - **双指针法的实战应用**:通过快慢指针(Fast-Slow Pointer)实现高效。慢指针标记唯一元素位置,快指针遍历数组检测重复项,时间复杂度优化至$O(n)$,空间复杂度为$O(1)$[^2][^5]。 - **边界控制能力**:训练处理数组边界(如空数组、单元素数组)及连续重复元素的场景,避免索引越界[^1][^4]。 #### 2. **理解数据结构特性** - **数组的原地操作**:实验要求"原地修改"数组(不新建数组),直接覆盖重复元素,降低内存开销,符合高性能系统开发需求[^1][^5]。 - **有序数组的利用**:依赖数组已排序的特性,只需比较相邻元素($nums[i]$ vs $nums[i+1]$),简化重复项判断逻辑[^2]。 #### 3. **应用于实际工程场景** - **数据优化**:为数据库索引清理、日志压缩、时间序列数据处理等提供高效方案[^1]。 - **算法扩展基础**:例如"允许元素最多保留两次"的变体(见LeetCode 80题),通过修改指针判断条件(如$nums[index-2]$)实现灵活控制[^3]。 #### 4. **验证算法正确性** - **测试标准化**:实验需通过系统测试用例,验证新长度$k$与预期数组$expectedNums$完全匹配,确保结果严格一致[^5]。 > **示例代码逻辑**(引用自LeetCode): > ```c > int removeDuplicates(int* nums, int numsSize) { > if (numsSize == 0) return 0; > int slow = 0; > for (int fast = 1; fast < numsSize; fast++) { > if (nums[fast] != nums[slow]) { > slow++; // 移动慢指针 > nums[slow] = nums[fast]; // 覆盖重复项 > } > } > return slow + 1; // 返回唯一元素数量 > } > ``` ### 总结 该实验旨在通过双指针法训练**高效操作有序数组**的能力,培养**边界条件处理**思维,并为实际数据场景提供算法基础,是面试与工程实践的核心考点[^1][^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值