数组——RemoveDuplicatesfromSortedArray-创新互联

描述

成都创新互联长期为上千多家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为榆中企业提供专业的成都网站设计、成都做网站,榆中网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。

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].

要求时间复杂度为O(n),空间复杂度为O(1)

#include <iostream>
#include <assert.h>
#include <vector>
#include <algorithm>
using namespace std;

class Solution
{
public:
	int removeDuplicates(char *a, size_t len)
	{
		assert(a);
		size_t index = 1;
		int first = 0;
		int second = 1;
		while (second < len){
			if (a[second] != a[first]){
				a[index++] = a[second];
				first = second;
			}
			second++;
		}
		return index;
	}
};

以上是我自己看完题目后所编写的程序

分析:

len是数组元素的个数

first为第一个元素下标,second为第二个元素下标(如果数组只有一个元素,则不会进入循环,而是直接返回1)

index为复制后数组的个数

运行结果:

测试数组为 char a[16] = { 1, 1, 1, 2, 2, 2,2,5 ,6,6,6,6,7,7,8,9};

数组——Remove Duplicates from Sorted Array

以下是参考LeetCode中使用STL实现的代码

代码1:

class Solution
{
public:
	int removeDuplicates(char a[], size_t len)
	{
		return distance(a, unique(a, a + len));
	}
};

所使用的函数:

template <class ForwardIterator>
  ForwardIterator unique ( ForwardIterator first, ForwardIterator last );
 distance (InputIterator first, InputIterator last);

代码2:

class Solution
{
public:
	int removeDuplicates(char a[], size_t len)
	{
		return _removeDuplicates(a,a+len,a)-a;
	}
	template<class T1,class T2>
	T2 _removeDuplicates(T1 first, T1 last, T2 output)
	{
		while (first != last){
			*output++ = first;
			first = upper_bound(first,last,*first);
		}
		return output;
	}
};

所使用的函数:

template <class ForwardIterator, class T>
  ForwardIterator upper_bound ( ForwardIterator first, ForwardIterator last,
                                const T& value );

================================================================

描述

Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice?

For example, Given sorted array A = [1,1,1,2,2,3],

Your function should return length = 5, and A is now [1,1,2,2,3]

要求时间复杂度为O(n),空间复杂度为O(1)

class Solution2
{
public:
	int removeDuplicates(int a[], size_t len)
	{
		return _removeDuplicates(a,a+len,a)-a;
	}
	template<class T1,class T2>
	T2 _removeDuplicates(T1 first, T1 last, T2 output)
	{
		T1 tmp = first;
		while (first != last){
			if ((tmp!=first-1)&&(*tmp == *(first - 1))){
				*output++ = *(first - 1);
			}
			*output++ = *first;
			tmp = first;
			first = upper_bound(first,last,*first);
		}
		return output;
	}
};

以上代码是我自己根据上题中使用STL进行部分代码修改实现成功的程序

运行结果:

测试数组为 int a[16] = { 1, 1, 1, 2, 2, 2,2,5 ,6,6,6,6,7,7,8,9};

数组——Remove Duplicates from Sorted Array

以下是参考LeetCode中实现的代码

代码1:

class Solution2
{
public:
	int removeDuplicates(int *a,size_t len)
	{
		size_t index = 2;
		for (int i = 2; i < len ; ++i){
			if (a[i] != a[index - 2])
				a[index++] = a[i];
		}
		return index;
	}
};

代码2:

class Solution2
{
public:
	int removeDuplicates(int *a, size_t len)
	{
		int index = 0;
		for (int i = 0; i < len ; ++i){
			if (i>0 && i<len-1 && a[i] == a[i - 1] && a[i] == a[i + 1])
				continue;
			a[index++] = a[i];
		}
		return index;
	}
};

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

分享标题:数组——RemoveDuplicatesfromSortedArray-创新互联
网站网址:https://www.cdcxhl.com/article6/ceocog.html

成都网站建设公司_创新互联,为您提供网站营销品牌网站设计品牌网站建设静态网站动态网站ChatGPT

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

h5响应式网站建设