// interviewproblem.cpp : Defines the entry point for the console application.
//
//整形数组 {1,1,2,2,3,3,4,4,5,5} ,去掉重复数结果是{1,2,3,4,5}并返回个数5。
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
int RemoveRepeat(int *a, int size)
{
int end, last;//end是修改后不重复的数组中最后一个元素的下标 last为当前搜索的重复的值
end = last = -1;
for (int i = 0; i < size; i++)
{
if (a[i] != last)
{
a[++end] = a[i];//将不重复的值复制到不重复数组的末尾
last = a[i];//更新要搜索的不重复的值
}
}
return end + 1;
}
int main()
{
int a[10] = { 1,1,2,2,3,3,4,4,5,5};
int size = sizeof(a) / sizeof(int);
cout << "before remove repeat: " << endl;
for (int i = 0; i < size; i++)
cout << a[i]<<" ";
int newsize = RemoveRepeat(a, size);
cout << endl;
cout << "after remove repeat: " << endl;
for (int i = 0; i < newsize; i++)
{
cout << a[i] << " ";
}
cout << endl;
system("pause");
return 0;
}
去除相同的字母
最新推荐文章于 2024-03-06 14:35:19 发布