LeetCode刷题:删除有序数组的重复项详解

题目

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 A = [1,1,2],
Your function should return length = 2, and A is now [1,2].

解法1:常规解法

/*
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 A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
*/

// 时间复杂度n,空间复杂度1
#include<iostream>
using namespace std;
#include<vector>
class Solution{
	public:
		int removeDuplicates(vector<int>& nums){
			if(nums.empty()) return 0;
			int index=0;
			for(int i=0;i<nums.size();i++){
				// 先自己和自己比较
				// 建议画图
				if(nums[index]!=nums[i]){
					// index是用来更新数组的
					// 只要发现后面的某个元素和当前指向的元素值不一样
					// 就会去更新数组
					index+=1; 
					
					nums[index]=nums[i];
					
				}
			}
			return index+1;
		}
};
int main(){
	Solution s;
	vector<int> array={1,2,2,3,3};
	int result=s.removeDuplicates(array);
	cout<<result;
	return 0;
}

解法2:STL懒人解法

//使用stl
// 时间复杂度是n,空间复杂度是1
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution{
	public:
		int removeDuplicates(vector<int>& nums){
//			printf("%d %d",*nums.begin(),*nums.end());
			return distance(nums.begin(),unique(nums.begin(),nums.end()));
			/*
			begin()函数返回一个指向当前vector起始元素的迭代器.
			end() 函数返回一个指向当前vector末尾元素的下一位置的迭代器.注意,如果你要访问末尾元素,需要先将此迭代器自减1.
			可以用其进行一些迭代操作,比如:
			  for (auto it = begin(myvector); it != end(myvector); ++it)
			        cout << *it << ' ';
			        
			unique()函数删除链表中所有重复的元素。如果指定pr,则使用pr来判定是否删除。
			distance() 函数用于计算两个迭代器表示的范围内包含元素的个数
			这段代码的意义就是,计算出没有重复元素数组的长度
			*/

		}
};
int main(){
	Solution s;
	vector<int> array={1,2,2,3,3};
	int result=s.removeDuplicates(array);
	cout<<result;
	return 0;
}

解法3:STL 勤快人模板解法

这就是解法1和解法2的综合

//使用stl
// 时间复杂度是n,空间复杂度是1
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution{
	public:
		int removeDuplicates(vector<int>& nums){
			return distance(nums.begin(),removeDuplicates(nums.begin(),nums.end(),nums.begin()));
		}
		template<typename InIt,typename OutIt>
//		template<class InIt,class OutIt>
/*	其中template和class是关键字,class可以用typename 关键字代替,在这里typename 和class没区别,
<>括号中的参数叫模板形参,模板形参和函数形参很相像,模板形参不能为空。*/
		OutIt removeDuplicates(InIt first,InIt last,OutIt output){
			while(first!=last){
				*output++=*first;
				first=upper_bound(first,last,*first);
				//upper_bound() 函数定义在<algorithm>头文件中,用于在指定范围内查找大于目标值的第一个元素。
				//查找[first, last)区域中第一个大于 val 的元素。
				// 这个方法和第一个方法类似
			}
			return output;
		}
};
int main(){
	Solution s;
	vector<int> array={1,2,2,3,3};
	int result=s.removeDuplicates(array);
	cout<<result;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zeker62

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值