如何找到一定数量的传递就像参数最大的序列? [英] How to find the biggest sequence of some number passed like parameter?
问题描述
我有一个数字矢量 的std ::向量号; 如何找到一定数量的传递就像参数最大的序列? 例如,如果我有像 0,1,1,3,2,0,0,0,6,0,0 我正在寻找的 0 号码我需要开始指数 5 在这种情况下。 有没有已经有一些功能或组合来实现的 STL 或提振作为这个问题? (我不能使用C ++ 11)
I have vector of numbers std::vector numbers; How to find the biggest sequence of some number passed like parameter ? For example if I have like 0, 1, 1 , 3 , 2 , 0 , 0, 0, 6 , 0 , 0 and I am looking for 0 number I need to get start index 5 in this case. Is there already some functions or combinations to achieve in stl or boost for this problem ? ( I cannot use C++11 )
推荐答案
我不认为这是一个标准的函数,它为你,但你可以设定一个相对简单的算法是:
I do not think there is a standard function that does this for you, but you can program a relatively straightforward algorithm for it:
int max_len = -1;
int best_index = -1;
int count = 0;
vector<int> data;
int value;
// Set the data and value here...
for (int i = 0 ; i != data.size() ; i++) {
if (data[i] == value) {
count++;
} else {
if (count > max_len) {
best_index = i - count;
max_len = count;
}
count = 0;
}
}
if (count > max_len) {
best_index = i - count;
max_len = count;
}
这篇关于如何找到一定数量的传递就像参数最大的序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!