例子: 简单题 小技巧:单指针遍历,记录状态 代码如下: class Solution { public int captureForts(int[] forts) { int pre = -1; int ans = 0; for(int i=0;i<forts.length;i++){ if(forts[i]==1 || forts[i]==-1){ if(pre>=0 && forts[i]!=forts[pre]){ ans = Math.max(ans,i-pre-1); } pre = i; } } return ans; } }