返回给定数组的一个版本,其中数组中的每个零值都被数组中零右侧的最大奇数值所替换

  • 本文关键字:数组 替换 一个 版本 返回 java arrays
  • 更新时间 :
  • 英文 :


如果零右边没有奇数,则将零保留为零。

zeroMax([0, 5, 0, 3]) → [5, 5, 3, 3]
zeroMax([0, 4, 0, 3]) → [3, 4, 3, 3]

这是来自CodingBat:https://codingbat.com/prob/p187050

当然有比我更好的实现,但这将极大地帮助我了解哪里出了问题。

findAndReplace方法没有完成它的工作。我看不出它为什么坚持intarray[0]=0的原因,这就是我被卡住的地方。我已经独立于这个类实现了这个方法,并且它按预期工作。

以下是我的作品:

public class ZeroMax {
public static int[] zeroMax(int[] intarray) {
int max = largestOdd(intarray);
System.out.println("largest odd is " + max);

return findAndReplace1(intarray, 0, max);
}
//method returns the largest odd value or returns zero
public static int largestOdd(int[] arr) {
int maxodd = 0;
int n = arr.length;
int temp = 0;
//this is just a bubble sort
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (arr[j - 1] > arr[j]) {
//swap elements
temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
}
}
//this finds the largest number that is an odd
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] % 2 != 0) {
maxodd = arr[i];
break;
} else {
continue;
}
}
return maxodd;
}
//following returns an array where the zeros (int find)
// can be replaced with the largest odd (int replace)
public static int[] findAndReplace1(int[] intarray, int find, int replace) {
for (int i = 0; i < intarray.length; i++) {
//System.out.println(intarray[i]);
if (intarray[i] == find) {
intarray[i] = replace;
}
}
return intarray;
}
}

我认为问题的关键是

…数组中零的右边

一个给定的例子是zeroMax([0,5,0,3](→[5,5,3,3]。在代码中,您将在整个数组中找到最大的奇数值。在这种情况下为5。然后将数组中的每个0替换为5。

Original array:  [0, 5, 0, 3]
Expected result: [5, 5, 3, 3]
Your result:     [5, 5, 5, 3]

看来你还有一些编码工作要做

肯定有比我更好的实现,…

您的实现、设计和代码风格都很好。只是遗憾的是,它没有正确地解决问题。

如何解决问题的想法以下内容在所有情况下都应该有效:

  1. 从结束数组向后迭代,直到第一个(因此是最右边的(奇数
  2. 如果数组中没有任何奇数,则结束
  3. 将奇数存储到一个变量中,该变量包含迄今为止遇到的最大奇数
  4. 继续从您到达的索引向后迭代到索引0。对于每个索引:
    1. 如果索引处的数字是奇数,并且大于迄今为止最大的奇数,则将该数字存储为最大奇数
    2. 如果数字为0,则将迄今为止最大的奇数存储到此索引处的数组中

我看错了这个问题。这是我在仔细阅读后提出的成功解决方案:

public int[] zeroMax(int[] intarray) {
int maxvalue = 0;
int index = 0;
for (int i = 0; i < intarray.length; i++) {
if (intarray[i] == 0) {
index = i;
//call max value method
maxvalue = maxvalue(intarray, index);
intarray[i] = maxvalue;
}
}
return intarray;
}
public int maxvalue(int[] intarray, int index) {
int maxvalue = 0;
for (int i = index; i < intarray.length; i++) {
if ((intarray[i] % 2 == 1) && (intarray[i] > maxvalue)) {
maxvalue = intarray[i];
}
}
return maxvalue;
}

您可以使用Arrays.stream(T[],int,int)方法从当前索引到末尾对该数组进行迭代,然后使用filter奇数并获得其中的max

public static void main(String[] args) {
int[] arr1 = {0, 5, 0, 3};
int[] arr2 = {0, 4, 0, 3};
int[] arr3 = {0, 3, 0, 4};
replaceZeros(arr1);
replaceZeros(arr2);
replaceZeros(arr3);
System.out.println(Arrays.toString(arr1)); // [5, 5, 3, 3]
System.out.println(Arrays.toString(arr2)); // [3, 4, 3, 3]
System.out.println(Arrays.toString(arr3)); // [3, 3, 0, 4]
}
private static void replaceZeros(int[] arr) {
// iterate over the indices of array
IntStream.range(0, arr.length)
// filter zero elements
.filter(i -> arr[i] == 0)
// for each zero iterate over the elements
// of array from the current index to the end
.forEach(i -> Arrays.stream(arr, i, arr.length)
// filter odd elements
.filter(e -> e % 2 != 0)
// take the max element and
// replace the current one
.max().ifPresent(e -> arr[i] = e));
}

最新更新