- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试解决这个 CodingBat 问题:
(This is a slightly harder version of the fix34 problem.) Return an array that contains exactly the same numbers as the given array, but rearranged so that every 4 is immediately followed by a 5. Do not move the 4's, but every other number may move. The array contains the same number of 4's and 5's, and every 4 has a number after it that is not a 4. In this version, 5's may appear anywhere in the original array.
fix45({5, 4, 9, 4, 9, 5}) → {9, 4, 5, 4, 5, 9}
fix45({1, 4, 1, 5}) → {1, 4, 5, 1}
fix45({1, 4, 1, 5, 5, 4, 1}) → {1, 4, 5, 1, 1, 4, 5}
我最初使用的方法通过了所有站点测试,但我认为它不适用于较长的数组。最初的方法使用了 2 个循环并且没有使用新数组。我创建了一个解决方案,它引入了一个新数组和第三个嵌套循环,我相信它适用于该问题的所有实例。但是,该站点指出本节中的问题可以通过 2 个循环解决,所以我想知道是否真的有一个 2 循环解决方案可以解决任何问题的实例。这是问题和我的 3 循环解决方案:
public int[] fix45(int[] nums) {
int[] locations = {-1};
for (int i = 0; i < nums.length - 1; ++i) {
if (nums[i] == 4) {
JLoop:
for (int j = nums.length-1; j >= 0; --j) {
if (nums[j] == 5) {
for (int k = locations.length-1; k>=0 ; --k) {
if (locations[k] == j) {
continue JLoop;
}
}
nums[j] = nums[i + 1];
nums[i + 1] = 5;
locations[locations.length - 1] = i+1;
locations = java.util.Arrays.copyOf(locations,
locations.length + 1);
locations[locations.length-1] = -1;
break;
}
}
}
}
return nums;
}
最佳答案
每次找到 4 时都从数组的一端重新开始搜索合适的 5 似乎很浪费。数组的一部分已经被扫描并且已知不包含可以移动的 5。这是 O(n) 的时间和 O(1) 的空间。
public static int[] fix45(int[] nums) {
int j = 0;
for (int i = 0; i < nums.length - 1; ++i) {
if (nums[i] == 4 && nums[i + 1] != 5) {
/*
* Need to find the next movable 5 That means an element that is 5 and
* either is the first element or is preceded by anything other than 4
*/
while (nums[j] != 5 || (j != 0 && nums[j - 1] == 4)) {
j++;
}
nums[j] = nums[i + 1];
nums[i + 1] = 5;
}
}
return nums;
}
关于java - Codingbat fix45 有更简单的解决方案吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13337515/
我显然缺少一个“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
我是一名优秀的程序员,十分优秀!