(Java refers to offer) Build a product array

1. Question analysis

Given an array A[0,1,...,n-1], construct an array B[0,1,...,n-1], wherein the element B
B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]. Division cannot be used.
(Note: Provisions
B[0] = A[1] * A[2] * ... * A[n-1],B[n-1] = A[0] * A[1] * ... * A[n-2];)
For the case where the length of A is 1, B has no meaning and therefore cannot be constructed, so this situation will not exist.

The key to the problem is to deal with the first, last, and middle positions, Three situations need to be considered

Two, code one

import java.util.Arrays;

/**
 * @Auther: Yolo
 * @Date: 2020/9/9 08:29
 * @Description:
 */
public class Test_09 {
    
    
    public static void main(String[] args) {
    
    
        int[] A = {
    
    1, 2, 3, 4,5};
        int[] B = multiply(A);
        System.out.println(Arrays.toString(B));
    }

    private static int[] multiply(int[] A) {
    
    
        //A 数组不满足题意
        if (A.length <= 1 || A == null) {
    
    
            return null;
        }
        //初始化 b 数组
        int[] b = new int[A.length];
        for (int i = 0; i < A.length; i++) {
    
    
            int temp = 1;
            
            if (i == 0) {
    
    
                //处理 A[0]的情况
                for (int j = 1; j < A.length; j++) {
    
    
                    temp = temp * A[j];
                }
            } else if (i == A.length - 1) {
    
    
                //处理 A[n-1] 的情况
                for (int j = 0; j < A.length - 1; j++) {
    
    
                    temp = temp * A[j];
                }
            } else {
    
    
                //处理其余的情况
                for (int j = 0; j < A.length; j++) {
    
    
                    if (i - 1 >= j || j >= i + 1) {
    
    
                        temp = temp * A[j];
                    }
                }
            }
            b[i] = temp;
        }
        return b;
    }
}

Three, code two

The value of B[i] can be seen as the product of each row in the matrix in the figure below.

The lower triangle can be easily obtained by continuous multiplication, and the upper triangle is also continuous multiplication from bottom to top.

So our thinking is very clear, first calculate the continuous multiplication in the lower triangle, that is, we first calculate a part of B[i], and then reverse according to the distribution law in the upper triangle, and multiply the other part.

Insert picture description here

public class Solution {
    
    
    public int[] multiply(int[] A) {
    
    
        int length = A.length;
        int[] B = new int[length];
        if(length != 0 ){
    
    
            B[0] = 1;
            //计算下三角连乘
            for(int i = 1; i < length; i++){
    
    
                B[i] = B[i-1] * A[i-1];
            }
            int temp = 1;
            //计算上三角
            for(int j = length-2; j >= 0; j--){
    
    
                temp *= A[j+1];
                B[j] *= temp;
            }
        }
		return B;
    }
}

Four, summary

Array initialization: It int[] b = new int[A.length];
cannot be simple and direct int[] b=A;, so that they both point to the same address. Modifying b will also cause A to change

Guess you like

Origin blog.csdn.net/nanhuaibeian/article/details/108481474