如何在一组1和0中找到一组的起始和结束指标
How to find the beginning and ending indices of groups of ones in a set of ones and zeros
如何找到一组的起始和结束索引?我尝试了一些复杂的嵌套if语句,但没有成功。有没有一个算法来处理这类问题,或者它有一个名字。谢谢。
int arr[32] = {0,1,1,1,1,0,0,0,
1,1,1,0,0,0,0,0,
1,1,0,1,0,0,0,1,
0,0,0,0,0,1,1,1};
我怀疑这个算法是否有特定的名称,但这里有一个变体要考虑:
int i = 0;
while (i < 32) {
while (i < 32 && arr[i] == 0) i++;
if (i < 32) {
cout << "Found start index " << i << endl;
while (i < 32 && arr[i] == 1) i++;
cout << "Found end index " << (i - 1) << endl;
}
}
// of course move 32 to local variable outside the loop
的主要思想:跳过所有的0,如果我们还没有到达数组的末尾-找到所有的1,重复
相关文章:
- 在 c++ 中拥有一组结构的正确方法是什么?
- 有哪些有效的方法可以消除一组 100 万个字符串>重复数据?
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 一组值的零开销下标运算符
- 使用一组结构,避免在一组结构中出现重复的结构
- CMake:我们可以为一组不形成可执行文件或库的特定文件指定包含目录吗?
- 合并一组模板专用化
- 如何更好地检查两个 char 变量是否在一组值中?
- C++有没有办法强制重写一组方法,如果其中一个方法在子类中具有重写?
- 给定一个枢轴点,按照它们与枢轴点构成的角度递增顺序对一组点进行排序
- 如何从一组变量中查找最低值
- 如何根据对的第二个元素对 STL c++ 中的一组对进行排序?
- 如何通过查找迭代器结果分配给一组对的元素
- 读取一组用户输入,按升序排序,然后打印结果
- 如何从一组具有从左到右优先级的整数值创建有序整数键?
- 在 c++ 中定义一组 set 的迭代器
- 使用 C++20 概念强制类实现一组方法
- 通过在上一个数组结束后立即存储下一个元素来扩展数组
- 如何在一组1和0中找到一组的起始和结束指标
- 对于原始数组和std::vector,明显没有对下标进行过一次结束的说明.这个问题已经得到了决定性的解决吗?