【问题标题】:Largest product in a series (java)系列中最大的产品(java)
【发布时间】:2018-10-21 09:06:02
【问题描述】:

在给定 9 位数字的情况下,我试图找到产生最大产品的 3 个相邻数字。例如对于数字 198348756,我的解决方案应该返回 [8,7,5],因为它的乘积是 280,并且是可能的最大乘积。我的代码找到了最大乘积,但无法返回创建最大乘积的数字数组。相反,它返回它在 for 循环中检查的最后一个数组。我不明白为什么创建最大乘积的数字数组没有存储在变量结果中?

  public class Solution {
  ArrayList<Integer> digits = new ArrayList<>();


  /// digits to int array
  void tocharArray(String num) {
    char[] charArray = num.toCharArray();

    for (Character c : charArray) {
      digits.add(Character.getNumericValue(c));
    }
    //System.out.println(digits);
    //System.out.println(digits.size());
  }


  //gets product of array ex [1,2,3] ->6
  int arrayproduct(ArrayList<Integer> array) {
    int product = 1;
    for(int i=0;i < array.size(); i++) {
      product = product * array.get(i);
    }
    return product;
  }


  ArrayList<Integer> func() {
    ArrayList<Integer> three = new ArrayList<>();
    ArrayList<Integer> result = new ArrayList<>();


    // array of the first 3 digits of the number
    for(int index = 0; index < 3;index++) {
      three.add(digits.get(index));
    }

    //initially the max product is the first 3 digits 
    int maxproduct = arrayproduct(three); 
    System.out.println(three); //from test [1,9,8]
    System.out.println(maxproduct);// from test 72

    ArrayList<Integer> copy = three;

    for(int j = 3 ; j < digits.size();j++) {

      copy.remove(0);
      copy.add(digits.get(j));
      int next = arrayproduct(copy);
      System.out.println(copy);

      if(next > maxproduct) {
        maxproduct = next;
        result = copy;
      }
    }
    System.out.println(maxproduct); // returns 280 which is correct 
    System.out.println(result); // returns [7,5,6]

    return result;

  }

  public static void main(String[] args) {
    String test1 = "198348756";

    Solution sol = new Solution();
    sol.tocharArray(test1); \\[1,9,8,3,4,8,7,5,6]
    sol.arrayproduct(sol.digits); \\returns [7,5,6] which is incorrect
    sol.func();
  }
}

【问题讨论】:

    标签: java algorithm math numbers series


    【解决方案1】:

    问题出在这一行:

    ArrayList<Integer> copy = three;
    

    您的代码假定copy 是一个副本,但实际上它是同一three 列表的别名

    您可以通过实际复制列表来修复它:

    ArrayList<Integer> copy = new ArrayList<>(three);
    

    result = copy 也是如此 - 它必须是 result = new ArrayList&lt;&gt;(copy)

    注意:您可以通过更改arrayproduct 来进一步简化此代码以获取初始索引和长度。这样,您就可以避免在进行时创建新列表。一旦找到正确的位置,您就可以通过调用 subList 来制作三元素子范围的副本。

    【讨论】:

    • 你是对的,但是 result = copy 和 result = new ArrayList(copy) 有什么区别?我无法理解这两个语句如何引用不同的数据?
    • @ken24ny,参考ArrayList.java的源码,new ArrayList&lt;&gt;(copy)会复制底层数组,并返回一个新的arrayList依赖于它。
    【解决方案2】:

    更简单的方法

    ArrayList<Integer> getMaxConsecutive(ArrayList<Integer> arr)
    {
        ArrayList<Integer> result = new ArrayList<Integer>();
    
        int start=-1;
        int max=INT_MIN;
        for(int i=0;i<(arr.size()-3);i++)
        {
             int sum = arr.get(i)+arr.get(i+1)+arr.get(i+2);
             if(sum>max){
                 max=sum;
                 start = i;
             }
        }
    
        result.add(arr.get(start));
        result.add(arr.get(start+1));
        result.add(arr.get(start+2));
    
        return result;
    }
    

    【讨论】:

    • 好的,因为你可以用 * 替换 + ,它会像一个魅力一样工作
    【解决方案3】:

    来自源代码:

    for (int j = 3; j < digits.size(); j++) {
    
        copy.remove(0);
        copy.add(digits.get(j));
        int next = arrayproduct(copy);
        System.out.println("-->" + next);
        System.out.println("-->" + copy);
    
        if (next > maxproduct) {
            maxproduct = next;
            result = copy;
            System.out.println("result-->" + result);
        }
    }
    

    变量 copy 和 result 是对象引用,就像 C 的指针一样。 因此,每当 maxproduct 值更改时,您需要将副本内容的所有内容显式复制(克隆)到结果对象。有几种方法可以完成此方法。

    使用克隆方法是一种简单的方法;

    result = copy;
    

    result = (ArrayList<Integer>)copy.clone();
    

    所以,func方法如下:

    @SuppressWarnings("unchecked")
    ArrayList<Integer> func() {
        ArrayList<Integer> three = new ArrayList<>();
        ArrayList<Integer> result = new ArrayList<>();
    
        // array of the first 3 digits of the number
        for (int index = 0; index < 3; index++) {
            three.add(digits.get(index));
        }
    
        // initially the max product is the first 3 digits
        int maxproduct = arrayproduct(three);
        System.out.println(three); // from test [1,9,8]
        System.out.println(maxproduct);// from test 72
    
        ArrayList<Integer> copy = three;
    
        for (int j = 3; j < digits.size(); j++) {
    
            copy.remove(0);
            copy.add(digits.get(j));
            int next = arrayproduct(copy);
            System.out.println(copy);
    
            if (next > maxproduct) {
                maxproduct = next;
                result = (ArrayList<Integer>)copy.clone();
            }
        }
        System.out.println(maxproduct); // returns 280 which is correct
        System.out.println(result); // returns [7,5,6]
    
        return result;
    
    }
    

    那么输出消息可能是你所期望的。

    [1, 9, 8]
    72
    [9, 8, 3]
    [8, 3, 4]
    [3, 4, 8]
    [4, 8, 7]
    [8, 7, 5]
    [7, 5, 6]
    280
    [8, 7, 5]
    

    好好编码……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      • 2015-10-07
      • 1970-01-01
      相关资源
      最近更新 更多