gpt4 book ai didi

java - 其值是每个其他整数的乘积的数组

转载 作者:行者123 更新时间:2023-12-01 13:13:16 25 4
gpt4 key购买 nike

我在电话中被问到以下面试问题:

Given an array of integers, produce an array whose values are the product of every other integer 

excluding the current index.

Example:

[4, 3, 2, 8] -> [3*2*8, 4*2*8, 4*3*8, 4*3*2] -> [48, 64, 96, 24]

我给出了以下答案

import java.math.BigInteger;
import java.util.Arrays;

public class ProductOfAnArray {

public static void main(String[] args) {

try {
System.out.println(Arrays.toString(ProductOfAnArray
.calcArray(new int[] { 4, 3, 2, 8 })));
System.out.println(Arrays.toString(ProductOfAnArray
.calcArray(new int[] { 4, 0, 2, 8 })));
System.out.println(Arrays.toString(ProductOfAnArray
.calcArray(new int[] { 4, 0, 2, 0 })));
System.out.println(Arrays.toString(ProductOfAnArray
.calcArray(new int[] {})));
System.out
.println(Arrays.toString(ProductOfAnArray
.calcArray(new int[] { 4, 3, 2, 8, 3, 2, 4, 6, 7,
3, 2, 4 })));
System.out
.println(Arrays.toString(ProductOfAnArray
.calcArray(new int[] { 4, 3, 2, 8, 3, 2, 4, 6, 7,
3, 2, 4 })));
System.out.println(Arrays.toString(ProductOfAnArray
.calcArray(new int[] { 4432432, 23423423, 34234, 23423428,
4324243, 24232, 2342344, 64234234, 4324247,
4234233, 234422, 234244 })));
} catch (Exception e) {
// debug exception here and log.
}
}

/*
* Problem: Given an array of integers, produce an array whose values are
* the product of every other integer excluding the current index.
*
* Assumptions. Input array cannot be modified. input is an integer array
* "produce an array" - type not specified for output array
*
* Logic explanation:
*
* Assume we have array [a,b,c,d] Let multiple be multiple of each element
* in array. Hence multiple = 0 if one of the element is 0; To produce the
* output. Ans at i -> multiple divided by the value at i. if 2 numbers are
* 0 then entire output will be 0 because atleast one 0 will be a multiple
* if 1 number is 0 then every thing else will be 0 except that index whole
* value is to be determined
*
*/
public static BigInteger[] calcArray(final int[] inp) throws Exception {
if (inp == null)
throw new Exception("input is null");

int cnt = 0;
BigInteger multiple = new BigInteger("1");
boolean foundZero = false;

for (int i : inp) {
if (i == 0) {
cnt++;
foundZero = true;
if (cnt < 2)
continue;
else
break;
}
multiple = multiple.multiply(BigInteger.valueOf(i));
}

BigInteger ans[] = new BigInteger[inp.length];

for (int i = 0; i < inp.length; i++) {
if (foundZero) {
if (cnt < 2) {
ans[i] = (inp[i] == 0) ? multiple : new BigInteger("0");
} else {
ans[i] = new BigInteger("0");
}
} else {
ans[i] = multiple.divide(BigInteger.valueOf(inp[i]));
}
}
return ans;
}

}

但是我没有被选中。我想获得对我的答案的反馈并看看有什么问题。

谢谢。

最佳答案

我过去也尝试过同样的方法。没有选择:)。我试图在第一个循环中捕获零索引(如果有的话)。并简单地将乘积分配给该索引(我使用双数组,默认值为 0) - 因此如果找到 0,则无需再次迭代。

并在第一个循环中检查乘积是否为无穷大Double.isInfinite,如果是,则中断循环 - 没有必要找到剩余的乘积(假设输入是大量的大数字)

public static double[] arrayProducts(int[] input) {
int length = input.length;
double product = 1;
boolean zeroFound = false;
int zeroIndex = 0;
double[] output = null;
for (int i = 0; i < length; i++) {
if (input[i] == 0) {
if (zeroFound) {
throw new ProductArrayException(0, true, zeroIndex,
ZERO_FOUND_EXPECTION);
}
zeroFound = true;
zeroIndex = i;
} else {
product = product * input[i];
if (Double.isInfinite(product)) {
throw new ProductArrayException(0, true, zeroIndex,
INFINITY_FOUND_EXPECTION);
}
}
}

if (zeroFound) {
throw new ProductArrayException(product, false, zeroIndex,
ZERO_FOUND_EXPECTION);
} else {
output = new double[length];
for (int i = 0; i < length; i++) {
output[i] = product / input[i];
}
}
return output;
}

关于java - 其值是每个其他整数的乘积的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22672331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com