2023-09-02力扣每日一题

链接:

2511. 最多可以摧毁的敌人城堡数目

题意和解:

阅读理解题,要从1到-1或-1到1,中间只能有0,求最多能有多少0

实际代码:

c++ 复制代码
#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
相关推荐
Dream it possible!3 小时前
LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)
算法·leetcode·面试·动态规划
心抵鹊4 小时前
算法之双指针系列(一)
算法·leetcode
Pniubi6 小时前
力扣560.和为k的子数组
算法·leetcode·职场和发展
Pniubi7 小时前
力扣438.找到字符串中所有字母异位词
算法·leetcode·职场和发展
文丑颜不良啊11 小时前
LeetCode2081~2100题目解析
java·leetcode
文丑颜不良啊13 小时前
LeetCode2061~2080题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode1941~1960题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode1961~1980题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode2001~2020题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode2021~2040题目解析
java·leetcode