INPUT : [3,3,3,2,2,2,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0]
组重复在向量项 - C++
OUTPUT : [[3,3,3],[2,2,2],[1,1,1,1,1],[0,0,0,0,0,0,0,0,0,0]]
输入是为int的向量,而输出是整数的向量的向量。目标是以时间最有效的方式做到这一点。
我目前使用的解决方案是这样的:
vector<vector<int> results;
vector<int> result;
for(int i = 0 ; i < list.size() - 1 ; i++){
result.push_back(list[i]);
if (list[i] != list[i+1]){
results.push_back(result);
result.clear();
}
}
result.push_back(list[list.size()-1]);
results.push_back(result);
- 信贷:@kabanus
欢迎stackoverflow.com。请花些时间阅读[帮助页面](http://stackoverflow.com/help),尤其是名为[“我可以问些什么话题?”]的章节(http://stackoverflow.com/help/)讨论话题)和[“我应该避免问什么类型的问题?”](http://stackoverflow.com/help/dont-ask)。还请[参观](http://stackoverflow.com/tour)和[阅读如何提出好问题](http://stackoverflow.com/help/how-to-ask)。最后,请学习如何创建[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。 –
任何简单的工作算法都可能足够接近最高效。 – aschepler
如果你没有任何东西,最有效的就是任何有效的东西。写一些有用的东西,然后才开始考虑效率。 – user463035818