zoukankan      html  css  js  c++  java
  • 剑指Offer(Java版)第五十七题:给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1], 其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。 不能使用除法。(注意:规定B[0] = A[1] * A[2] * ... * A[n-1],B[n-1] = A[0] * A[1] * ... * A[n-2];)

    /*
    给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],
    其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。
    不能使用除法。(注意:规定B[0] = A[1] * A[2] * ... * A[n-1],B[n-1] = A[0] * A[1] * ... * A[n-2];)
    */

    import java.util.ArrayList;

    public class Class57 {

    public int[] multiply(int[] A){
    if(A == null || A.length <= 0){
    return A;
    }
    int[] B = new int[A.length];
    B[0] = 1;
    int temp = 1;
    for(int i = 1; i < A.length; i++){
    B[i] = B[i - 1] * A[i - 1];
    }
    for(int j = A.length - 2; j >= 0; j--){
    temp = temp * A[j + 1];
    B[j] = B[j] * temp;
    }
    return B;
    }

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    }

    }

  • 相关阅读:
    Python2.7-math, cmath
    Python2.7-pprint
    Python2.7-copy
    Python2.7-weakref
    Python2.7-Queue
    Python2.7-sched
    Python2.7-array
    Python2.7-bisect
    搜索专题:Balloons
    【洛谷P4460】解锁屏幕【状压dp】
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12542205.html
Copyright © 2011-2022 走看看