【问题标题】:Return a version of the given array where each zero value in the array is replaced by the largest odd value to the right of the zero in the array返回给定数组的一个版本,其中数组中的每个零值都被数组中零右侧的最大奇数值替换
【发布时间】:2020-12-10 04:34:42
【问题描述】:

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

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

【问题讨论】:

    标签: java arrays


    【解决方案1】:

    我相信你的问题的关键是

    …数组中零的右侧

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

    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,则将迄今为止最大的奇数存储到该索引处的数组中。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        您可以使用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));
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-06-08
          • 1970-01-01
          • 2018-10-01
          • 1970-01-01
          • 2019-02-20
          • 1970-01-01
          • 1970-01-01
          • 2016-04-20
          相关资源
          最近更新 更多