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