ruf*_*ang 0 java arrays syntax
例:
我有一个布尔数组,里面有N个布尔值.哪个是确定阵列中有多个连续M个真实标志的最有效方法?
写一些像(bA = booleanArray,M = 3)
(bA[0] && bA[1] && bA[2]) || (bA[1] && bA[2] && bA[3]) || ...
Run Code Online (Sandbox Code Playgroud)
真是太丑了.
并且这不能通过具有动态最小真实标志的VERY-LONG动态长度数组来满足需要.也许效率低下而且分散真实?
使用循环和计数器:
int count = 0;
for (boolean x: theArray){
if (x) {
count++;
if (count >= n) return true; // n is the count we are looking for
} else { count = 0; }
}
return false;
Run Code Online (Sandbox Code Playgroud)