简体   繁体   English

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

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

This is my code: 这是我的代码:

 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])); 

This code's job is to: 此代码的工作是:

Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array. 给定一系列整数作为数组,请确定是否有可能通过从数组中删除不超过一个元素来获得严格递增的序列。

Example

For sequence = [1, 3, 2, 1] , the output should be 对于sequence = [1, 3, 2, 1] ,输出应为

almostIncreasingSequence(sequence) = false;

There is no one element in this array that can be removed in order to get a strictly increasing sequence. 为了获得严格递增的顺序,此数组中没有一个元素可以删除。

For sequence = [1, 3, 2] , the output should be 对于sequence = [1, 3, 2] ,输出应为

almostIncreasingSequence(sequence) = true.

You can remove 3 from the array to get the strictly increasing sequence [1, 2] . 您可以从数组中删除3以获得严格递增的序列[1, 2] Alternately, you can remove 2 to get the strictly increasing sequence [1, 3] . 或者,您可以删除2以获得严格递增的序列[1, 3]

But it doesn't work correctly even after a bunch of edits. 但是,即使经过一堆编辑,它也无法正常工作。 Currently, these are the arrays that the code doesn't work correctly on: 当前,这些是代码无法在其上正确运行的数组:

  • [1, 3, 2, 1] It returns true instead of false. [1, 3, 2, 1]返回true而不是false。

  • [1, 2, 5, 5, 5] It returns true instead of false. [1, 2, 5, 5, 5]它返回true而不是false。

  • [1, 2, 3, 4, 3, 6] It returns false instead of true. [1, 2, 3, 4, 3, 6]它返回false而不是true。

I'm editing this code long time ago, every time I add something/edit something, a problem gets fixed but another problem gets messed up and it keeps like that. 很久以前,我正在编辑此代码,每次添加内容/编辑内容时,一个问题都会得到解决,但另一个问题却变得一团糟,并且一直保持这种状态。

EDIT: Please tell me why it doesn't work, don't give me the right code please. 编辑:请告诉我为什么它不起作用,请不要给我正确的代码。

 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])); 

I wanted to make this, from a now deleted post , as the right answer: 我想从现在已删除的帖子中做出正确的答案:

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]));
}

The most performant way is to go through the sequence only once. 最有效的方法是只执行一次序列。 This code remembers the previous number and checks if the current one is higher. 该代码会记住先前的数字,并检查当前数字是否更高。 If it is lower or equal, then it marks the higher number as the mistake, so the next number can be higher again. 如果它小于或等于,则将较高的数字标记为错误,因此下一个数字可以再次较高。

 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])) 

This seems to work 这似乎有效

  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