我有一些数组,我想计算数组中零序列的数量。
例如:
A [0][0][1][1][1][0][0][0][2][2][2][2][0][0][0]
将返回 3 个序列
我怎样才能有效地做到这一点?
编辑:我有兴趣用来解决问题的语言是
Javascript
最佳答案
您可以只计算后面跟着非零或数组末尾的零。例如,在 Java 中:
int result = 0;
for (int i = 0; i < A.length; ++i) {
if (A[i] == 0 && (i + 1 == A.length || A[i+1] != 0)) {
++result;
}
}
return result;
关于javascript - 检测数组中的零序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42917525/