- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
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, 5, 2], 2) → [1, 3, 3, 5, 5, 2]
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;
}
当我在 codingbat 上运行它时,它适用于除此之外的所有示例:notAlone([1, 2, 3, 2, 5, 2], 2) 应该返回 [1, 3, 3, 5, 5, 2],但我的返回 [1, 3, 3, 3, 5, 2] ].
我真的很困惑如何解决这个问题,因为在我看来,我所写的内容也应该适用于这个特定的示例,但显然事实并非如此。我的错误从何而来?我应该如何重写我的代码?非常感谢任何帮助!
最佳答案
你把它复杂化了。如果要替换当前元素,则只需要找到上一个和下一个元素的 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;
}
顺便说一句,在您的解决方案中,您忽略了给定值 (val
):
Return a version of the given array where every instance of the given value which is alone is replaced...
不过,这不是您的代码在给定情况下失败的原因。您只是没有找到正确的最大值:
当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];
如果您将这 5 行替换为:
int max = nums[k-1];
if(nums[k+1] > max)
max = nums[k+1];
你会得到正确的输出。
关于java Codingbat notAlone — 为什么它不适用于这个特定示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53459768/
我显然缺少一个“if”语句,但我不知道如何涵盖茶或糖果不是彼此的两倍: Java > Logic-1 > teaParty 我们正在举办一个有大量茶和糖果的聚会。返回编码为 0=bad、1=good
我正在尝试编码 bat 问题repeatFront: Given a string and an int n, return a string made of the first n characte
这是问题; http://codingbat.com/prob/p153748 给定两个 int,每个都在 10..99 范围内,如果有一个数字出现在两个数字中,例如 12 和 23 中的 2,则返回
我正在 codingbat 上解决这个问题它表明我的代码适用于所有情况,但不适用于其他情况。 Given an array of ints, return true if the value 3 ap
我目前正在 codingbat 网站上做一个练习,上面写着: Given an array of ints, compute recursively if the array contains a 6
我正在尝试解决 this CodingBat问题: Squirrels who like to party get together and smoke cigars. Such a party is
对于模棱两可的标题,我深表歉意,我想不出更具体的东西。 为了更好地递归解决问题,我一直在处理 CodingBat 上发布的问题.我的问题与以下问题的变体有关。 original problem是: G
给定任务sameEnds来自 CodingBat: 给定一个字符串,返回出现在字符串开头和结尾且不重叠的最长子字符串。例如,sameEnds("abXab") 是 "ab"。 sameEnds("ab
我正在codingbat.com 上解决Java 问题,并且在这个问题上停止了,scoresAverage。由于练习描述有点复杂,我就直接粘贴在这里: Given an array of scores
我正在做codingbat作为我即将进行的测验的练习。我正在使用递归来解决递归问题,但我的老师说我应该能够使用其他循环来解决这些问题。我认为我应该使用 for 循环,因为它们可以轻松实现相同的结果。
嘿伙计们,我希望有人能向我解释这段代码中的错误?我只是有点难以理解为什么它会抛出异常。 最佳答案 第一个条件检查字符串的最小长度,应为 3。因此“bad”和“xba”通过第一个条件。 让我们先讨论ba
这类似于我之前的努力(wordEnds 和 repeatEnd):作为一种脑力练习,我想仅使用正则表达式来解决这个玩具问题。 Description from codingbat.com : Give
所以基本上,我一直在研究这些 codingBat 问题,当我真的遇到困难时,我通常会检查解决方案并跟踪逻辑,这帮助我不会陷入后来使用类似想法的问题。 这个最大镜像问题对我个人来说不像其他问题;我不知道
我开始学习使用 Java。 我在codingbat ( http://codingbat.com/prob/p123384 ) 中遇到了一个问题:这是我的代码 public String frontB
这是我的任务:给定一个字符串,“xyz”是否出现在字符串的中间?为了定义 middle,我们会说“xyz”左侧和右侧的字符数最多只能相差 1。 使用下面的代码可以看到问题描述和其他用例中的失败here
下面是我用于 no_teen_sum 和后续 fixed_teen 函数的代码。 第一个代码是我提交的 - 并且适用于所有测试用例: def no_teen_sum(a, b, c): # che
我有点困惑为什么我的下面的解决方案没有给出正确的答案。我做了一些挖掘,我猜这与调用的工作方式有关?我认为这两种方式是相同的,但事实并非如此,但我不完全理解我的错误返回的是什么。这是我之前做的研究:ht
如果字符串“cat”和“dog”在给定字符串中出现相同的次数,则返回 True。 这是我正在尝试的问题。我的代码是: def cat_dog(str): count1 = 0 coun
我正在尝试解决这个 CodingBat 问题: (This is a slightly harder version of the fix34 problem.) Return an array th
我试图尽可能多地理解正则表达式,所以我想出了这个基于正则表达式的解决方案来解决 codingbat.com repeatEnd : Given a string and an int N, retur
我是一名优秀的程序员,十分优秀!