我通过在代码信号中处理不同的问题来挑战自己。在几乎增加的序列问题中,我对此进行了编码:
function almostIncreasingSequence(sequence) {
var count = 0;
sequence.forEach((element, i) => {
if(sequence[i] <= sequence[i-1]){
count++;
if(sequence[i] <= sequence[i-2] && sequence[i+1] <= sequence[i-1]){
return false;
}
}
});
return count <= 1;
};
14/17答案是正确的。但是,在寻找解决方案时,我发现使用普通的for循环执行此操作是可行的,而且我不知道为什么
function almostIncreasingSequence(sequence) {
var count = 0;
for(i=0; i < sequence.length; i++){
if(sequence[i] <= sequence[i-1]){
count++;
if(sequence[i] <= sequence[i-2] && sequence[i+1] <= sequence[i-1]){
return false;
}
}
}
return count <= 1;
};