zoukankan      html  css  js  c++  java
  • 面试题66:构建乘积数组

    这道题真是扩展思维啊,我服了:

    import java.util.Arrays;
    
    /**
     * Created by clearbug on 2018/2/26.
     */
    public class Solution {
    
        public static void main(String[] args) {
            Solution s = new Solution();
            System.out.println(Arrays.toString(s.multiply(new int[]{1, 2, 3, 4, 5, 6})));
        }
    
        public int[] multiply(int[] arr) {
            int[] res = new int[arr.length];
            res[0] = 1;
    
            for (int i = 1; i < arr.length; i++) {
                res[i] = res[i - 1] * arr[i - 1];
            }
    
            int temp = arr[arr.length - 1];
            for (int i = arr.length - 2; i >= 0; i--) {
                res[i] *= temp;
                temp *= arr[i];
            }
    
            return res;
        }
    }
    
  • 相关阅读:
    正则
    cookie、sesion
    POJ-1509
    HDU-3374
    ZOJ-3822
    HDU-5492
    在什么情况下Java比C++快?
    HDU-5451
    SPOJ-913
    莫比乌斯反演入门
  • 原文地址:https://www.cnblogs.com/optor/p/8643681.html
Copyright © 2011-2022 走看看