Java codingbat maxMirror练习的问题

Java codingbat maxMirror练习的问题,java,Java,因此,基本上,我一直在经历这些编码问题,当我真的陷入困境时,我通常会检查解决方案并跟踪逻辑,这有助于我避免在以后使用类似想法的问题上陷入困境 这个max mirror问题与我个人的其他问题不同;我不知道如何实际编写代码来解决它,即使形成算法对我来说也有点棘手 我们可以说,数组中的镜像部分是一组连续元素,在数组中的某个位置,相同的组以相反的顺序出现。例如,{1,2,3,8,9,3,2,1}中最大的镜像部分是长度为3的{1,2,3}部分。返回给定数组中找到的最大镜像部分的大小 maxMirror({

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

这个max mirror问题与我个人的其他问题不同;我不知道如何实际编写代码来解决它,即使形成算法对我来说也有点棘手

我们可以说,数组中的镜像部分是一组连续元素,在数组中的某个位置,相同的组以相反的顺序出现。例如,{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(如果不是)。但就伪代码和真实代码而言,我不知道。

就性能而言,这绝对不是最好的解决方案。欢迎进一步改进

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; 
}

就性能而言,这绝对不是最好的解决方案。欢迎进一步改进

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; 
}

在这种情况下,我的解决方案是,代码应该总是手动执行,然后找出解决方案的本质

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

接下来,我把它翻译成伪代码

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;
}

在这种情况下,我的解决方案是,代码应该总是手动执行,然后找出解决方案的本质

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

接下来,我把它翻译成伪代码

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;
}

我的两分钱

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]);
}

我的两分钱

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]);
}

解决方案是简单的,而不是复杂的:

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;
}

解决方案是简单的,而不是复杂的:

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;
}

以下是我的答案,希望评论能解释清楚:

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
}

以下是我的答案,希望评论能解释清楚:

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
}

虽然这个答案可能是正确和有用的,但如果你想解释一下它是如何帮助解决问题的,那就更好了。如果有一个可能不相关的变化导致它停止工作,读者需要了解它曾经是如何工作的,那么这在将来会变得特别有用。虽然这个答案可能是正确和有用的,但如果你解释它如何帮助解决问题,你会更喜欢这个答案。如果有一个可能不相关的变化导致它停止工作,并且读者需要了解它曾经是如何工作的,那么这在将来变得特别有用。