codingbat maxMirror练习的问题

时间:2014-07-28 00:18:57

标签: java

所以基本上,我一直在解决这些编码问题,当我真的陷入困境时,我通常会检查解决方案并跟踪逻辑,这有助于我不会遇到后来使用类似想法的问题

这个最大的镜子问题与我个人不一样;我不知道如何实际编写代码来解决它,即使形成算法对我来说也是一件棘手的事情

  

我们会说"镜像"数组中的一个部分是一组连续的元素,这样在数组中的某个地方,相同的组以相反的顺序出现。例如,{1, 2, 3, 8, 9, 3, 2, 1}中最大的镜像部分是长度3({1, 2, 3}部分)。返回给定数组中找到的最大镜像部分的大小。

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,如果不是的话。但就伪代码和真实代码而言,我不知道。

5 个答案:

答案 0 :(得分:2)

我在这样的情况下去寻求解决方案,你的代码应该做的事情总是手动完成,然后找出解决方案的本质。

对于这个问题,我发现自己正在查看原始数组的可能子集,然后向后查看原始数组,看看我是否可以再次找到相同的子集。

接下来,我将其翻译成伪代码,

for each segment in nums
   check if nums contains segment backwards

重复,但这一次有更多的实施细节。

for each segment in nums, starting with the largest
   reverse the segment
   check if nums contains reversed segment
   if it does, return the size of that segment

接下来,找到一些可能的伪代码方法候选者并编写它们。我选择这样做是为了“反向”和“包含”:

private int[] reverse(int[] nums) {
   int[] rtn = new int[nums.length];
   for (int pos = 0; pos < nums.length; pos++) {
      rtn[nums.length - pos - 1] = nums[pos];
   }
   return rtn;
}

private boolean contains(int[] nums, int[] segment) {
   for (int i = 0; i <= nums.length - segment.length; i++) {
      boolean matches = true;
      for (int j = 0; j < segment.length; j++) {
         if (nums[i + j] != segment[j]) {
            matches = false;
            break;
         }
      }
      if (matches) return true;
   }
   return false;
}

最后,实施其余的:

public int maxMirror(int[] nums) {
   for (int window = nums.length; window > 0; window--) {
      for (int pos = 0; pos <= nums.length - window; pos++) {

         int[] segment = new int[window];
         for (int innerpos = 0; innerpos < window; innerpos++) {
            segment[innerpos] = nums[pos + innerpos];
         }

         segment = reverse(segment);
         if (contains(nums, segment)) {
            return window;
         }
      }
   }
   return 0;
}

答案 1 :(得分:1)

我无关紧要的两分钱......

public int maxMirror(int[] nums) {

  // maximum mirror length found so far
  int maxlen= 0;

  // iterate through all possible mirror start indexes
  for (int front = 0; front < nums.length; front++) {

    // iterate through all possible mirror end indexes
    for (int back = nums.length - 1; back >= front; back--) {

      // this inner for-loop determines the mirror length given a fixed
      // start and end index          

      int matchlen = 0;
      Boolean match = (nums[front] == nums[back]);

      // while there is a match
      //  1. increment matchlen
      //  2. keep on checking the proceeding indexes
      while (match) {

        matchlen++;

        int front_index = front + matchlen;
        int back_index = back - matchlen;

        // A match requires
        //  1. Thee indexes are in bounds
        //  2. The values in num at the specified indexes are equal
        match =
          (front_index < nums.length) &&
          (back_index >= 0) &&
          (nums[front_index] == nums[back_index]);
      }

      // Replace the max mirror length with the new max if needed
      if (matchlen > maxlen) maxlen = matchlen;
    }
  }

  return maxlen;
}

旨在让您感到困惑的替代解决方案

public int maxMirror(int[] nums) {
  return maxlen_all_f(nums, 0);
}

int maxlen_all_f(int [] nums, int f) {
  return (f >= nums.length)
    ? 0
    : max(
        maxlen_for_start_f(nums, f, nums.length - 1),
        maxlen_all_f(nums, f + 1)
      );
}

int max(int a, int b){
  return (a > b)
    ? a
    : b;
}

int  maxlen_for_start_f(int [] nums, int f, int b) {
 return (b < f)
   ? 0
   : max(
       matchlen_f(nums, f, b),
       maxlen_for_start_f(nums, f, b - 1)
     );
}

int matchlen_f(int[] nums, int f, int b) {
  return match_f(nums, f, b)
    ? 1 + matchlen_f(nums, f + 1, b - 1)
    : 0;
}

Boolean match_f(int [] nums, int a, int b) {
  return (a < nums.length && b >= 0) && (nums[a] == nums[b]);
}

答案 2 :(得分:1)

解决方案很简单,而不是使其变得复杂:

public static int maxMirror(int[] nums) {
    final int len=nums.length;
    int max=0;
    if(len==0)
        return max;
    for(int i=0;i<len;i++)
    { 
        int counter=0;
        for(int j=(len-1);j>i;j--)
        {
            if(nums[i+counter]!=nums[j])
            {
                break;
            }
            counter++;
        }

        max=Math.max(max, counter);
    }

    if(max==1)
        max=0;      
    return max;
}

答案 3 :(得分:0)

这绝对不是性能方面的最佳解决方案。邀请进一步改进。

public int maxMirror(int[] nums) {
 int maxMirror=0;

 for(int i=0;i<nums.length;i++)
 {
   int mirror=0;
   int index=lastIndexOf(nums,nums[i]);
   if(index!=-1){
    mirror++;

    for(int j=i+1;j<nums.length;j++)
    {
       if(index>=0&&existsInReverse(nums,index,nums[j]))
       {      
         mirror++;
         index--;
         continue;

        }
        else 
         break; 
    }

    if(mirror>maxMirror)
     maxMirror=mirror;
   }
 }

 return maxMirror; 
}


int lastIndexOf(int[] nums,int num){
 for(int i=nums.length-1;i>=0;i--)
 {
   if(nums[i]==num)
    return i;
 }
 return -1;
}

boolean existsInReverse(int nums[],int startIndex,int num){
  if(startIndex!=0&&(nums[startIndex-1]==num))
   return true;
  return false; 
}

答案 4 :(得分:0)

这是我的答案,希望评论能很好地解释它:)

public int maxMirror(int[] nums) {
  int max = 0;
  // our largest mirror section found stored in max

  //iterating array 
  for(int i=0;i<nums.length;i++){
    int iterator = i; // iterator pointing at one element of array
    int counter = 0;//counter to count the mirror elements

    //Looping through for the iterator element
    for(int j=nums.length-1;j>=i;j--){

      //found match i.e mirror element
      if(nums[iterator] == nums[j]){
        iterator++; // match them until the match ends
        counter++; // counting the matched ones
      }
       else{
            //matching ended
                  if(counter >= max){//checking if previous count was lower than we got now
                      max = counter; // store the count of matched elements
                  }
                  counter = 0; // reset the counter
                  iterator = i; // reset the iterator for matching again
              }
    }
    if(counter >= max){//checking if previous count was lower than we got now
      max = counter;// store the count of matched elements at end of iteration
    }

  }

  return max;//return count
}