几乎增加了序列 [英] Almost increasing sequence
问题描述
/*Given a sequence of integers as an array(vector), determine whether it is possible to obtain a strictly
* increasing sequence by removing no more than one element from the array.*/
#include <iostream>
#include <vector>
using namespace std;
bool almostIncreasingSequence(std::vector<int> sequence)
{
int count=0;
int i=0;
while(i<sequence.size())
{
if(sequence[i]>sequence[i+1])
{
count++;
}
i++;
}
if(count>1)
{
return false;}
else
{
return true;
}
i++;
}
int main()
{
vector<int>arr{1,6,5,8,9};
cout<<almostIncreasingSequence(arr)<<endl;
}
我的尝试:
我正在尝试正确编译此代码,但我不明白我的错误是什么
What I have tried:
I am trying to compile this code correctly, but I don't understand what is my mistake
推荐答案
你当前的问题是
You immediate problem is
while(i< sequence.size())
while(i<sequence.size())
你有将其更改为
You have to change it to
while(i < (sequence.size() - 1) )
然而这还不够。
First所有你必须在比较中用> =
替换>
(因为它应该是严格的增加序列),然后你应该尝试删除 sequence [i]
或 sequence [i + 1]
,接受第一个保留 incresing 属性的本地。最后'你可以处理风格点',例如返回 false
每当 count> 1
而不是等待循环结束时。
However that's not enough.
First of all you have to replace >
with >=
in comparisons (because it should be a strictly increasing sequence), then you shoud try to remove either sequence[i]
or sequence[i+1]
, accepting the first one that preserves locally the incresing property. Lastly 'you can work on points of style' e.g. return false
whenever count>1
instead of waiting the end of the loop.
这篇关于几乎增加了序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!