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

    }

    }

  • 相关阅读:
    bzoj 1004 burnside 引理+DP
    bzoj 3453 数论
    HDU 2899 三分
    HDU 2199 二分
    bzoj 3450 DP
    bzoj 1197 DP
    bzoj 2121 DP
    bzoj 2258 splay
    bzoj 1296 DP
    Memcached的限制和使用建议
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12542205.html
Copyright © 2011-2022 走看看