作为我正在从事的工作的一部分,我现在正在尝试让我的函数在找到连续最长 0 的第一个实例时进行记录 - 例如:
holLength([1,1,0,1,0,0,1]); // starts at index 4
holLength([0,0,1,0,0,0,1,1,0]); // starts at index 3
holLength([1,0,0,1,0,0,1,1]); // starts at index 1
holLength([1,0,0,1,1,1,0,0,1]); // starts at index 1
holLength([1,0,0,1,0,0,1]); // starts at index 1
holLength([0,0,1,1,1,0,1]); // starts at index 0, and is WRONG
holLength([0,1,1,0,0,1,1]); // starts at index 3
我无法让 holLength([0,0,1,1,1,0,1]);
工作,因为 changeCount
最初需要有值为-1,否则其他测试失败。
有人知道如何解决这个问题吗?
理想情况下,我希望它能够说明 0 结束后连续有更多 1 的索引,例如:
holLength([1,0,0,1,0,0,1,1]); // starts at index 4
holLength([1,0,0,1,1,0,0,1]); // starts at index 1
但是先修复第一部分可能更简单!任何帮助和最佳编码实践建议将不胜感激,非常感谢。
代码:
function holLength(arr) {
if(!(arr.includes(0) && arr.includes(1) && arr.join('').includes('01'))) {
throw new RangeError('Bad Input - Function Aborted');
} else {
var oneIndexes = [], zeroIndexes = [], longest1s = 0, longest0s = 0, zeros = 0, ones = 0, len = arr.length, holStart = 0, changeCount = -1, first1 = 0, nextOnes = 0;
createIndexes();
console.log('zeroIndexes: ' + zeroIndexes + '\noneIndexes: ' + oneIndexes);
for(i = 0; i < zeroIndexes.length; i++) { // for each zero indexes in arr
console.log('Arr Index: ' + zeroIndexes[i] + ', arr[i]: ' + arr[i]);
for(j = arr[zeroIndexes[i]]; j < len; j++) { // go through each 0 value
let next = arr[j+1];
let current = arr[j];
let thisIndex = zeroIndexes[i];
let nextIndex = zeroIndexes[i+1];
if(thisIndex === 0 && current === 0) {
zeros++;
// changeCount++;
} else if (thisIndex === 0 && current === 1) {
ones++;
}
if(next === 0 && nextIndex !== len) { // if next original array value = 0 & not last one
zeros++;
ones = 0;
if (zeros > longest0s) {
longest0s = zeros;
console.log('j: ' + j + ', longest0s changed. Changed from index ' + (j - changeCount) + '. changeCount: ' + changeCount);
changeCount++;
holStart = (j - changeCount + 1);
}
console.log(' zeros: ' + zeros + ', longest0s: ' + longest0s + ', j: ' + j);
} else if (next === 1 && nextIndex !== len) { // if 1 & not last
ones++;
zeros = 0;
if (ones > longest1s) {
longest1s = ones;
console.log('longest1s changed. Changed from index ' + j); // wrong? cant be j?
}
console.log(' ones: ' + ones + ', longest1s: ' + longest1s + ', j: ' + j);
}
}
console.log('==========');
}
first1 = holStart + longest0s;
console.log('first1: ' + first1);
console.log('hol starts at index: ' + holStart + ' for ' + longest0s + ' days in the north');
// for loop - use while instead?
for (i = first1; i < len; i++) {
let next = arr[i+1];
let nextIndex = zeroIndexes[i+1];
if (next === 1 && nextIndex !== len) {
nextOnes++;
console.log('nextOnes: ' + nextOnes);
}
}
// ===== FUNCTIONS =====
function createIndexes() {
for(i = 0; i < len; i++) { // add all 0 & 1 indexes into arrays
pushIndexes(i, 0);
pushIndexes(i, 1);
}
}
function pushIndexes(i, no) {
if (no === 1 && arr[i] === no) {
oneIndexes.push(i);
}
if (no === 0 && arr[i] === no) {
zeroIndexes.push(i);
}
}
return (longest0s + nextOnes);
}
}
console.log(holLength([1,1,0,1,0,0,1])); // starts at index 4
console.log(holLength([0,0,1,0,0,0,1,1,0])); // starts at index 3
console.log(holLength([1,0,0,1,0,0,1,1])); // starts at index 1
console.log(holLength([1,0,0,1,1,1,0,0,1])); // starts at index 1
console.log(holLength([1,0,0,1,0,0,1])); // starts at index 1
console.log(holLength([0,0,1,1,1,0,1])); // starts at index 0, and is WRONG
console.log(holLength([0,1,1,0,0,1,1])); // starts at index 3
最佳答案
如果将逻辑从“连续有多少个零/一”更改为“连续有多少个相等的值”,它会变得更容易:
function occurences(array) {
const counts = {}, max = {};
let count = 0, start = 0, current = array[0];
for(const [index, el] of array.entries()) {
if(el === current) {
count++;
} else {
(counts[current] || (counts[current] = [])).push({ count, start, end: index - 1});
if(!max[current] || count > max[current].count) max[current] = { start, count, end: index - 1 };
count = 1; current = el, start = index;
}
}
return { count, max };
}
所以你可以将它用作:
const result = occurences([1,1,0,0,0,1,0,0]);
console.log(
result.max[0].start, // start position of the maximum 0 row
result.max[1].count // maximum number of ones in a row
);
关于javascript - 条件为 true 的数组的日志索引(有尽可能多的 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51189415/