链接:
2511. 最多可以摧毁的敌人城堡数目
题意和解:
阅读理解题,要从1到-1或-1到1,中间只能有0,求最多能有多少0
实际代码:
#include<bits/stdc++.h>
using namespace std;
int captureForts(vector<int>& forts)
{int ans=0,after=1E8+7;bool Left=false;for(int i=0;i<forts.size();i++){if(forts[i]==1){if(!Left) ans=max(ans,i-after-1);after=i;Left=true;}else if(forts[i]==-1){if(Left) ans=max(ans,i-after-1);after=i;Left=false;}}return ans>=0?ans:0;
}
int main()
{vector<int> forts;int fort;while(cin>>fort) forts.push_back(fort);int ans=captureForts(forts);cout<<ans<<endl;return 0;
}
限制:
1 <= forts.length <= 1000
-1 <= forts[i] <= 1