简体   繁体   English

java Codingbat notAlone - 为什么它不能用于这个特定的例子

[英]java Codingbat notAlone — why doesn't it work for this specific example

We'll say that an element in an array is "alone" if there are values before and after it, and those values are different from it. 如果在它之前和之后有值,那么我们会说数组中的元素是“单独的”,并且这些值与它不同。 Return a version of the given array where every instance of the given value which is alone is replaced by whichever value to its left or right is larger. 返回给定数组的一个版本,其中给定值的每个实例单独替换为其左侧或右侧的较大值。

notAlone([1, 2, 3], 2) → [1, 3, 3] notAlone([1,2,3],2)→[1,3,3]

notAlone([1, 2, 3, 2, 5, 2], 2) → [1, 3, 3, 5, 5, 2] notAlone([1,2,3,2,5,2],2)→[1,3,3,5,5,2]

notAlone([3, 4], 3) → [3, 4] notAlone([3,4],3)→[3,4]

public int[] notAlone(int[] nums, int val) {
  for(int k = 1 ; k<nums.length; k++)
  {
    if(k!= nums.length-1)
    {
      int max = nums[k];
      if(nums[k-1]>nums[k])
        max = nums[k-1];
      else if(nums[k+1] > nums[k])
        max = nums[k+1];
      if(nums[k-1] != nums[k] && nums[k] != nums[k+1])
        nums[k] = max;
    }
  }
  return nums;
}

When I ran this on codingbat, it worked for all examples except for this: notAlone([1, 2, 3, 2, 5, 2], 2) should return[1, 3, 3, 5, 5, 2], but instead mine returned[1, 3, 3, 3, 5, 2]. 当我在codingbat上运行它时,它适用于除此之外的所有示例:notAlone([1,2,3,2,5,2],2)应该返回[1,3,3,5,5,2],但我的回归[1,3,3,3,5,2]。

I am really stuck on how to solve this, because in my mind, what I've written should work for this specific example as well, but apparently it doesn't. 我真的被困在如何解决这个问题上,因为在我看来,我所写的内容也适用于这个特定的例子,但显然它没有。 Where does my error come from? 我的错误来自哪里? How should I re-write my code? 我该如何重新编写代码? Any help would really be appreciated! 真的很感激任何帮助!

You're over complicating it. 你复杂化了。 You only need to find the max of the previous and next elements if the current element should be replaced: 如果要替换当前元素,您只需要找到上一个和下一个元素的max

public static int[] notAlone(int[] nums, int val) {
    for(int k = 1 ; k<nums.length - 1; k++)
    {
        if(nums[k]==val && nums[k-1] != nums[k] && nums[k] != nums[k+1])
            nums[k] = Math.max (nums[k-1], nums[k+1]);
    }
    return nums;
}

BTW, in your solution you ignore the given value ( val ): 顺便说一下,在你的解决方案中你忽略了给定的值( val ):

Return a version of the given array where every instance of the given value which is alone is replaced... 返回给定数组的一个版本,其中替换给定值的每个实例都是单独的...

That's not the reason why your code failed in the given case, though. 但这并不是你的代码在给定情况下失败的原因。 You simply didn't find the correct maximum: 您根本找不到正确的最大值:

When k==3 : k==3

int max = nums[k]; // max = 2
if(nums[k-1]>nums[k]) // 3 > 2
    max = nums[k-1]; // max = 3
else if(nums[k+1] > nums[k]) // no evaluated. therefore you change num[3] to 3 instead of to 5
    max = nums[k+1];

If you replaced these 5 lines with: 如果用以下代码替换这5行:

int max = nums[k-1];
if(nums[k+1] > max)
    max = nums[k+1];

you would have gotten the correct output. 你会得到正确的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM