客户要做网站建设话术,西宁网站建设最好的公司哪家好,制作app的专业公司,网站模板移植题目来源#xff1a; leetcode题目#xff0c;网址#xff1a;2511. 最多可以摧毁的敌人城堡数目 - 力扣#xff08;LeetCode#xff09;
解题思路#xff1a; 顺序遍历数组#xff0c;记录上一个我军城堡和没有城堡的位置。当碰到空位置时#xff0c;若上一次更新的…题目来源 leetcode题目网址2511. 最多可以摧毁的敌人城堡数目 - 力扣LeetCode
解题思路 顺序遍历数组记录上一个我军城堡和没有城堡的位置。当碰到空位置时若上一次更新的值为我军城堡记录较大的摧毁数当碰到我军城堡时若上一次更新的值为空位置记录较大的摧毁数。
解题代码
class Solution {public int captureForts(int[] forts) {int res0;int lastEmpty-1;int lastCommand-1;for(int i0;iforts.length;i){if(forts[i]-1){if(lastCommand!-1 lastCommandlastEmpty){resMath.max(res,i-lastCommand-1);}lastEmptyi;}else if(forts[i]1){if(lastEmpty!-1 lastEmptylastCommand){resMath.max(res,i-lastEmpty-1);}lastCommandi;}}return res;}
} 总结 注意不能越过我军城堡去往空地。 无官方题解。 fort 城堡、堡垒 capture 俘获采集拍摄