去除相同的字母

来源:互联网 发布:知乎提问 编辑:程序博客网 时间:2024/04/29 15:18
// 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;}
1 0