繁体   English   中英

为什么有时返回true而不是false导致我的代码无法正常工作?

[英]Why my code doesn't work correctly by sometimes returning true instead of false?

这是我的代码:

 function almostIncreasingSequence(sequence) { var counter = 0; for (var i = 1; i < sequence.length - 1; i++) { if (sequence[i] <= sequence[i - 1]) { counter += 1; } else if (sequence[i + 1] <= sequence[i - 1]) { counter += 1; } } if (counter <= 1) { return true; } return false; } console.log(almostIncreasingSequence([1, 3, 2, 1])); console.log(almostIncreasingSequence([1, 2, 5, 5, 5])); console.log(almostIncreasingSequence([1, 2, 3, 4, 3, 6])); 

此代码的工作是:

给定一系列整数作为数组,请确定是否有可能通过从数组中删除不超过一个元素来获得严格递增的序列。

对于sequence = [1, 3, 2, 1] ,输出应为

almostIncreasingSequence(sequence) = false;

为了获得严格递增的顺序,此数组中没有一个元素可以删除。

对于sequence = [1, 3, 2] ,输出应为

almostIncreasingSequence(sequence) = true.

您可以从数组中删除3以获得严格递增的序列[1, 2] 或者,您可以删除2以获得严格递增的序列[1, 3]

但是,即使经过一堆编辑,它也无法正常工作。 当前,这些是代码无法在其上正确运行的数组:

  • [1, 3, 2, 1]返回true而不是false。

  • [1, 2, 5, 5, 5]它返回true而不是false。

  • [1, 2, 3, 4, 3, 6]它返回false而不是true。

很久以前,我正在编辑此代码,每次添加内容/编辑内容时,一个问题都会得到解决,但另一个问题却变得一团糟,并且一直保持这种状态。

编辑:请告诉我为什么它不起作用,请不要给我正确的代码。

 function increasingSequence(sequence,i){ sequence = Array.prototype.slice.call(sequence); sequence.splice(i,1) var len = sequence.length; for(var i=0;i<len-1;i++){ if(sequence[i]>=sequence[i+1])return false; } return true; } function almostIncreasingSequence(sequence) { var len = sequence.length; for(var i=0;i<len-1;i++){ if(sequence[i]>=sequence[i+1]){ return increasingSequence(sequence,i)||increasingSequence(sequence,i+1) } } return true; } console.log(almostIncreasingSequence([1,2,3])); console.log(almostIncreasingSequence([1,3,2])); console.log(almostIncreasingSequence([1,2,3,3,2])); 

我想从现在已删除的帖子中做出正确的答案:

function almostIncreasingSequence(sequence) {
    return sequence.map((_, index) => [...sequence.slice(0, index), ...sequence.slice(index + 1)])
        .some(arr => arr.every((value, index) => index === 0 || value > arr[index - 1]));
}

最有效的方法是只执行一次序列。 该代码会记住先前的数字,并检查当前数字是否更高。 如果它小于或等于,则将较高的数字标记为错误,因此下一个数字可以再次较高。

 function isIncreasingWithMistakes(sequence) { prev = Number.MIN_VALUE; var mistakeCounter = 0; for (var number of sequence) { if (prev < number) { // The good case prev = number } else { // Found a mistake mistakeCounter += 1 prev = Math.min(prev, number) // Eliminate the higher number } // Stop immediately when there are too many mistakes if (1 < mistakeCounter) { return false } } return true } // These should be true console.log(isIncreasingWithMistakes([1, 2, 3, 4, 3, 6])) console.log(isIncreasingWithMistakes([10, 1, 2, 3, 4, 5])) // These should be false console.log(isIncreasingWithMistakes([1, 3, 2, 1])) console.log(isIncreasingWithMistakes([1, 2, 5, 5, 5])) console.log(isIncreasingWithMistakes([1,2,3,4,3,6,1])) 

这似乎有效

  function _doAlmostIncreasingSequence(_seq, recheck) { var warning = 0; var _control = _seq[0] - 1; for (var i = 0; i < _seq.length; i++) { var _test = _seq[i]; if (_test <= _control) { if (recheck) { var test1 = _seq.slice(0); var test2 = _seq.slice(0); test1.splice(i, 1); test2.splice(i - 1, 1); return _doAlmostIncreasingSequence(test1) || _doAlmostIncreasingSequence(test2); } return false; } _control = _test; } return true; } function almostIncreasingSequence(_seq) { return _doAlmostIncreasingSequence(_seq, 1); } console.log("TRUE :", almostIncreasingSequence([1, 2, 3, 5, 4, 5, 6])); console.log("TRUE :", almostIncreasingSequence([10, 1, 2, 3, 4, 5, 6])); console.log("TRUE :", almostIncreasingSequence([4, 5, 6, 6, 7, 8])); console.log("FALSE :", almostIncreasingSequence([4, 5, 6, 1, 2, 3])); console.log("TRUE :", almostIncreasingSequence([1, 3, 4, 6, 7, 8, 1, 10, 11, 12])); console.log("FALSE :", almostIncreasingSequence([1, 3, 2, 1])); console.log("FALSE :", almostIncreasingSequence([1, 2, 5, 5, 5])); console.log("TRUE :", almostIncreasingSequence([1, 2, 3, 4, 3, 6])); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM