【发布时间】:2017-10-18 11:19:52
【问题描述】:
INPUT : [3,3,3,2,2,2,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0]
OUTPUT : [[3,3,3],[2,2,2],[1,1,1,1,1],[0,0,0,0,0,0,0,0,0,0]]
输入是一个整数向量,而输出是一个整数向量的向量。目的是在所花费的时间方面以最有效的方式做到这一点。
我目前使用的解决方案是这样的:
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。请花一些时间阅读the help pages,尤其是名为"What topics can I ask about here?" 和"What types of questions should I avoid asking?" 的部分。也请take the tour 和read about how to ask good questions。最后请学习如何创建Minimal, Complete, and Verifiable Example。
-
任何简单的工作算法都可能足够接近最有效的算法。
-
如果你什么都没有,最有效的就是真正有效的东西。写一些有效的东西,然后才开始考虑效率。
-
我确实有一些东西但它不起作用,我无法找到错误。我将编辑问题以反映我所拥有的。 - 编辑:添加了我所拥有的。
-
作为替代方案 - 使用您的调试器并逐步完成它 - 更好的是,这个 :)