# Java solution: no division, we can use logarithm to "bypass".

• Not an elegant solution, just give another thought. We can use logarithm then minus to "bypass" division.
However, handling zeros and negative numbers became a little complicated.

``````public class Solution {
public int[] productExceptSelf(int[] nums) {
int prod = 1; // product
int zeroP = 0;  // position of zero
int zeroC = 0;  // zero counts
int nc = 1;  // negative number counts
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
zeroC++;
zeroP = i;
} else if (nums[i] < 0) {
prod *= - nums[i];
nc *= -1;
} else {
prod *= nums[i];
}
if (zeroC > 1) return new int[nums.length]; // If the array has two zeros, return array of zeros
}
if (zeroC == 1) {
int[] r = new int[nums.length];
r[zeroP] = nc * prod;
return r;
} // If the array has one zero, we have its position recorded, value as "prod".

for (int i = 0; i < nums.length; i++) {
if (nums[i] < 0) {
nums[i] = (int) Math.round((-nc * (Math.pow(10, (Math.log10(prod) - Math.log10(-nums[i]))))));
} else {
nums[i] = (int) Math.round((nc * (Math.pow(10, (Math.log10(prod) - Math.log10(nums[i]))))));
}
} // We cannot use devide, we use log and minus.
return nums;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.