老熟女激烈的高潮_日韩一级黄色录像_亚洲1区2区3区视频_精品少妇一区二区三区在线播放_国产欧美日产久久_午夜福利精品导航凹凸

重慶分公司,新征程啟航

為企業提供網站建設、域名注冊、服務器等服務

數組——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 
#include 
#include 
#include 
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 
  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
	T2 _removeDuplicates(T1 first, T1 last, T2 output)
	{
		while (first != last){
			*output++ = first;
			first = upper_bound(first,last,*first);
		}
		return output;
	}
};

所使用的函數:

template 
  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
	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

另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。


本文題目:數組——RemoveDuplicatesfromSortedArray-創新互聯
當前路徑:http://www.xueling.net.cn/article/ceocog.html

其他資訊

在線咨詢
服務熱線
服務熱線:028-86922220
TOP
主站蜘蛛池模板: 一级爰片 | 国产成人A人亚洲精V品无码 | 失禁大喷潮在线播放 | 国产激情视频一区二区 | 在线观看av你懂的 | 国产精品国产免费无码专区不卡 | 免费的api接口网站 殴美成人在线视频 | 久久国产精品不只是精品66 | 日日日干干干 | 色久视频在线播放 | 性欧美大战久久久久久久 | 国产精品久久久久久久久免费看 | 国产a免费| 久久久久黑人强伦姧人妻 | 免费观看很黄很色裸乳视频网站 | 日本精品免费看 | 国产农村寡妇一级毛片 | 国产欧美另类久久精品蜜芽 | 亚洲AV成人片色在线观看蜜桃 | 99蜜久久精品国产首页 | 另类内射国产在线 | 爆乳无码系列肉感在线播放 | 超碰国产欧美人人 | 放荡开放的人妻穿丁字裤凹 | 扒开两腿中间缝流白浆在线看 | 青娱乐极品视觉盛宴国产视频 | 99精品偷拍视频一区二区三区 | 色综合一区二区三区 | 国产亚洲一卡2卡3卡4卡网站 | 日韩无码偷拍中文字幕 | 一区二区三区国 | 国产中文日韩欧美 | 久久婷婷网站 | 91精品1区2区 | 强辱丰满的人妻HD高清3转4转 | 一级毛片在线看 | 毛茸茸av | 1区2区3区4区产品乱码入口 | 欧美kkkk7777免费看 | 69精品无码一区二区 | 国产精品99久久久久久人红楼 |