"Sword refers to offer study" "Face question 52: Build Product Array"

Source: Internet
Author: User

title: Given an array a[0,1,..., n-1], build an array b[0,1,..., n-1], where the elements in B b[i]=a[0]xa[1]x...xa[i-1]xa[i+1]x...xa[n-1], you cannot use division. Thinking of solving problems

For example: a[]={1,2,3} Beg b[]
B[0]=a[1]xa[2]=2x3=6
B[1]=a[0]xa[2]=1x3=3
b[2]=a[0]xa[1]=1x2=2

1.B[0] initialized to 1, starting from subscript I=1, first find out c[i] value and put b[i], that is b[i]=c[i]=c[i-1]xa[i-1], so b[1]=b[1-1]xa[1-1]=b[0]xa[0]=1x1=1,i++

2.b[2]=b[2-1]xa[2-1]=b[1]xa[1]=1x2=2,i++ out of length stop cycle

3.c[i], set a temporary variable temp to initialize to 1

4. From a forward variable array, lengtha=3 initializes the i=lengtha-2=1, ending with a condition of i>=0

5. First loop, temp=tempxa[i+1]=1xa[2]=3, calculates the value of the last element of a in temp,temp equivalent to D[i]

6. Because the previous b[i]=c[i], so let B[i]xd[i] is to save the result, that is b[i]=b[1]=b[1]xtemp=1x3=3,i–=0

7. Calculate B[i]=b[0],temp The value in the previous step is a[2], in this cycle temp=tempxa[0+1]=a[2]xa[1]=3x2=6

8.b[i]=b[0]=b0]xtemp=1x6=6,i–<0 Cycle End

So the b array is {6,3,2}

Code Implementation
Import Java.util.Arrays; Public classTest52 { Public Static Double[]Multiply(Double[] data) {if(Data = =NULL|| Data.length <2) {return NULL; }Double[] result =New Double[Data.length];//result[0] Take 1result[0] =1; for(inti =1; i < data.length; i++) {//First step each result[i] equals to Data[0]*data[1]...data[i-1]            //When i=n-1, the result of this time result[n-1] has been calculated "a"Result[i] = result[i-1] * Data[i-1]; }//tmp save DATA[N-1]*DATA[N-2]...DATA[I+1] Results        DoubleTMP =1;//second step for data[n-1]*data[n-2]...data[i+1]        the results of the//"A" result[n-1] have been calculated, so start operation from Data.length-2         for(inti = data.length-2; I >=0; i--) {tmp *= Data[i +1];        Result[i] *= tmp; }returnResult } Public Static void Main(string[] args) {Double[] Array1 = {1,2,3,4,5}; System. out. println (arrays.tostring (Multiply (array1)));//double expected[] = { -------        Double[] Array2 = {1,2,0,4,5}; System. out. println (arrays.tostring (Multiply (array2)));//double expected[] = {0, 0, 0, 0};        Double[] Array3 = {1,2,0,4,0}; System. out. println (arrays.tostring (Multiply (array3)));//double expected[] = {0, 0, 0, 0, 0};        Double[] Array4 = {1, -2,3, -4,5}; System. out. println (arrays.tostring (Multiply (ARRAY4)));//double expected[] = { -60 , +, -30, +};        Double[] Array5 = {1, -2}; System. out. println (arrays.tostring (Multiply (array5)));//double expected[] = {-2, 1};}}
Run Results

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Sword refers to offer study" "Face question 52: Build Product Array"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.