Java 值为其他整数乘积的数组
我在电话中被问到以下面试问题:Java 值为其他整数乘积的数组,java,arrays,multiplication,Java,Arrays,Multiplication,我在电话中被问到以下面试问题: 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.
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;
}
}
import java.math.biginger;
导入java.util.array;
公共类数组{
公共静态void main(字符串[]args){
试一试{
System.out.println(Arrays.toString)数组
.calcArray(新int[{4,3,2,8}));
System.out.println(Arrays.toString)数组
.calcArray(新int[]{4,0,2,8}));
System.out.println(Arrays.toString)数组
.calcArray(新int[]{4,0,2,0}));
System.out.println(Arrays.toString)数组
.calcArray(新int[]{}));
系统输出
.println(数组.toString(数组
.calcArray(新的int[]{4,3,2,8,3,2,4,6,7,
3, 2, 4 })));
系统输出
.println(数组.toString(数组
.calcArray(新的int[]{4,3,2,8,3,2,4,6,7,
3, 2, 4 })));
System.out.println(Arrays.toString)数组
calcArray先生(新界[]44324323423423428,
4324243, 24232, 2342344, 64234234, 4324247,
4234233, 234422, 234244 })));
}捕获(例外e){
//在此调试异常并记录日志。
}
}
/*
*问题:给定一个整数数组,生成一个值为
*除当前索引外的所有其他整数的乘积。
*
*假设。无法修改输入数组。输入为整数数组
*“生成数组”-未为输出数组指定类型
*
*逻辑解释:
*
*假设我们有数组[a,b,c,d],让multiple是每个元素的倍数
*因此,如果其中一个元素为0,则multiple=0;以生成
*i处的output.Ans->乘以i处的值。如果是2个数字
*0则整个输出将为0,因为至少有一个0将是倍数
*如果1是0,那么除了整个索引之外,其他所有的东西都是0
*价值有待确定
*
*/
公共静态BigInteger[]calcArray(final int[]inp)引发异常{
如果(inp==null)
抛出新异常(“输入为空”);
int-cnt=0;
BigInteger倍数=新的BigInteger(“1”);
布尔值foundZero=false;
用于(int i:inp){
如果(i==0){
cnt++;
foundZero=true;
if(cnt<2)
继续;
其他的
打破
}
multiple=multiple.multiple(BigInteger.valueOf(i));
}
BigInteger ans[]=新的BigInteger[inp.length];
对于(int i=0;i
但我没有被选中。我想得到关于我答案的反馈,看看有什么问题
谢谢。这可能是面试官想要的解决方案:
int[] arr = new int[]{4,3,2,8};
int[] newarr = new int[arr.length];
int product = 1;
for (int i=0; i < arr.length; i++) {
product = product * arr[i];
}
for (int i=0; i < arr.length; i++) {
newarr[i] = product / arr[i];
System.out.print(newarr[i] + " ");
}
int[]arr=newint[]{4,3,2,8};
int[]newarr=新int[arr.length];
int乘积=1;
对于(int i=0;i
我过去也用过同样的方法。未选择:)。
我试图在第一个循环中捕获零的索引(如果有的话)。只需将产品分配给该索引(我使用了双数组,默认值为0),因此如果找到一个0,则无需再次迭代
并在第一个循环中检查乘积为无穷大Double.isInfinite
,如果是,则中断循环-没有剩余的查找乘积点(假设输入是大容量的大数字)
公共静态双[]数组产品(int[]输入){
int length=input.length;
双积=1;
布尔值zeroFound=false;
int零指数=0;
double[]输出=null;
for(int i=0;i
给定整数数组arr[]
int[] arr = { 4, 3, 2, 8 };
int[] prod = new int[arr.length];
for (int i = 0; i < arr.length; i++)
{
prod[i] = 1;
for (int j = 0; j < arr.length; j++)
if (j != i) // Exclude element at the current index
prod[i] *= arr[j];
}
int[]arr={4,3,2,8};
int[]prod=新int[arr.length];
对于(int i=0;i
生成的产品数组是
prod[]
通常对于这个问题,不使用除法计算答案,它必须是一个有效的时间和空间算法
看下面哪个产生O(n)
int[] arr = { 4, 3, 2, 8 };
int[] prod = new int[arr.length];
for (int i = 0; i < arr.length; i++)
{
prod[i] = 1;
for (int j = 0; j < arr.length; j++)
if (j != i) // Exclude element at the current index
prod[i] *= arr[j];
}
public int[] otherIndexProduct(int array[]) {
int results[] = new int[array.length];
results[0] = 1;
for (int i = 0; i < array.length - 1; i++) {
results[i + 1] = results[i] * array[i];
}
int before = 1;
for (int i = array.length - 1; i >= 0; i--) {
results[i] *= before;
before *= array[i];
}
return results;
}
public int product(int array[], int post, int index) {
int prev = 1;
if (index < array.length) {
prev = product(array, post * array[index], index + 1);
int current = array[index];
array[index] = post * prev;
prev *= current;
}
return prev;
}