leetcode--题1
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int a[] = { 1,2,3,4,5,5,6,7,7,8,8,8,9,9,9,10,10 }; vector<int>res(a, a + 17); res.erase(unique(res.begin(), res.end()),res.end()); for (auto i : res) cout << i << ' '; system("pause"); return 0; }
解法二:
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int a[] = { 1,2,3,4,5,5,6,7,7,8,8,8,9,9,9,10,10 }; cout << a[0] << " "; for (int i = 1; i < 17; ++i) { if (a[i] == a[i - 1])continue; else cout << a[i] << ' '; } system("pause"); return 0; }