我正在努力解决这个问题MaxMirror在 CodingBat 上:
<小时/>We'll say that a "mirror" section in an array is a group of contiguous elements such that somewhere in the array, the same group appears in reverse order. For example, the largest mirror section in {1, 2, 3, 8, 9, 3, 2, 1} is length 3 (the {1, 2, 3} part). Return the size of the largest mirror section found in the given array.
maxMirror({1, 2, 3, 8, 9, 3, 2, 1}) → 3
maxMirror({1, 2, 1, 4}) → 3
maxMirror({7, 1, 2, 9, 7, 2, 1}) → 2
求解条件:
- 没有其他辅助方法。
- 不要在数组下使用 Java.util.Arrays.copyOf 或任何其他实用程序
- 不要使用集合。
我得到的解决方案适用于除 {7, 7, 7, 7, 6, 7, 7} 之外的所有情况,它应该返回 5,但我得到 6。我在这里做错了什么?
虽然 CodingBat 在运行时显示“全部正确”,但这是否也表明 CodingBat 没有检查所有可能的情况?
public int maxMirror(int[] nums) {
final int len=nums.length;
if(len==0)
return 0;
int maxCount=1;
boolean flag=false;
for(int i=0;i<len;i++)
{
int tempCount=1;
int count=i;
for(int j=len-1;j>=0&&(count<len);j--)
{
if((nums[count]==nums[j])&&!(flag))
{
flag=true;
count++;
continue;
}
if((nums[count]==nums[j])&&(flag))
{
tempCount++;
count++;
maxCount=(tempCount>maxCount)?tempCount:maxCount;
continue;
}
if(!(nums[i]==nums[j])&&(flag))
{
flag=false;
count=i;
tempCount=1;
continue;
}
if((j==count)||(j-count)==1)
{
flag=false;
break;
}
}
}
return maxCount;
}
最佳答案
此修复使得标志在外循环中未设置为 false,此修复不可见 here
关于java - 查找整数数组中存在的整数的最大镜像 [CodingBat 谜语 : MaxMirror],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24413007/