作为我正在做的事情的一部分,我正在尝试让我的函数暂时登录,直到它找到连续最长的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
答案 0 :(得分:1)
如果将逻辑从“连续多少个零/一”更改为“连续多少个相等的值”,则会变得更加简单:
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
);
答案 1 :(得分:1)
我要做的是跟踪您的最佳选择。 Javascript对象常量在这里很棒。
在这里,我跟踪多少个0,然后跟踪多少个1。然后在循环中检查是否比best
好。
这里是一个例子。
function holLength(arr) {
let best;
for (let start = 0; start < arr.length; start ++) {
if (arr[start] !== 0) continue;
let c;
for (c = start; c < arr.length; c ++) {
if (arr[c] !== 0) break;
}
const len = c - start;
let ones = 0;
for (; c < arr.length; c ++) {
if (arr[c] !== 1) break;
ones ++;
}
if (!best ||
len > best.len ||
(len === best.len && ones > best.ones))
{
best = {len, ones, start};
}
}
return best;
}
const ret = [
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
];
console.log(ret);
答案 2 :(得分:1)
您可以将一个对象用作临时结果,并在检查值的同时迭代数组。如果找到真实值,请检查,更新并重置计数器。否则,请检查计数器并获取索引以供以后存储。
最后返回索引。
function holLength(array) {
var result = { count: 0 },
count = 0,
index;
array.forEach((v, i) => {
if (v) {
if (result.count < count) {
result = { index, count };
}
count = 0;
return;
}
if (!count) {
index = i;
}
count++;
});
return result.index;
}
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
.as-console-wrapper { max-height: 100% !important; top: 0; }
首先从给定的索引开始计数。
function holLength(array) {
var result = { count: 0 },
count = 0,
index,
ones = array
.slice()
.reverse()
.map((s => v => s = v && s + 1)(0))
.reverse();
array.forEach((v, i) => {
if (v) {
if (result.count < count && ones[i] >= count) {
result = { index, count };
}
count = 0;
return;
}
if (!count++) {
index = i;
}
});
return result.index;
}
console.log(holLength([1, 0, 0, 1, 0, 0, 1, 1])); // starts at index 4
console.log(holLength([1, 0, 0, 1, 1, 0, 0, 1])); // starts at index 1
.as-console-wrapper { max-height: 100% !important; top: 0; }