消除相邻重复元素

#include <iostream>
#include <vector>
using namespace std;

vector<int> removeConsecutiveDuplicates(const vector<int>& arr) {
vector<int> result;
//如果原数组当前元素左右两个元素不同,将当前元素加入result
for (int i = 0; i < arr.size(); i++) {
if (i == 0 || arr[i] != arr[i-1]) {
if (i == arr.size()-1 || arr[i] != arr[i+1]) {
result.push_back(arr[i]);
}
}
}

return result;
}

int main() {
vector<int> arr = {1,1,1,2,3,3,2,5};
int size=arr.size();
vector<int> result = removeConsecutiveDuplicates(arr);
int after_size=result.size();
while(size!=after_size)
{
size=result.size();
result=removeConsecutiveDuplicates(result);
after_size=result.size();
}
// 打印结果
for (int num : result) {
std::cout << num << " ";
}

return 0;
}

消除相邻重复元素大于k的字母