This question already has answers here:
Given an array of numbers, return array of products of all other numbers (no division)
(45个答案)
3年前关闭。
通过电话向我询问以下面试问题:
我给出了以下答案
但是我没有被选中。我想获得我的答案的反馈,看看有什么问题。
谢谢。
(45个答案)
3年前关闭。
通过电话向我询问以下面试问题:
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)-因此,如果找到一个零,则无需再次进行迭代。
并在第一个循环中检查乘积是否为无穷大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;
}
10-05 18:30