#include <iostream> #include <vector> using namespace std; int solution(vector<int> & a){ int index = 0; for(int i = 1; i<a.size(); i++) { if (a[i] != a[index]){ a[++index] = a[i]; } } return index + 1; } int main(int argc, const char * argv[]) { vector<int> a = {1,2,4,5,6,7,7,8}; cout<< solution(a) <<endl; return 0; }
例如:[2,3,4,4,5,6,7]
经过算法处理之后应该是[2,3,4,5,6,7] 返回的是该数组的长度6。