java - 查找整数数组中存在的整数的最大镜像 [CodingBat 谜语 : MaxMirror]

标签 java arrays

我正在努力解决这个问题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

<小时/>

求解条件:

  1. 没有其他辅助方法。
  2. 不要在数组下使用 Java.util.Arrays.copyOf 或任何其他实用程序
  3. 不要使用集合。

我得到的解决方案适用于除 {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/

相关文章:

java - 如何将 String 或 CharSequence 的一部分转换为大写?

java - 超对称线算法?

java - Derby 数据库导出为单个文件?

arrays - 如何动态设置 Rust 数组长度?

c - C中的数组排序

Java递归如何

java - 如何编写 hello world servlet 示例

javascript - 导致错误的数组元素顺序

c# - 来自非托管内存的 byte[] 数组

javascript - 使用字符串作为对象字段来引用