# My solution beats 100% java solutions

• The idea is simply. The product basically is calculated using the numbers before the current number and the numbers after the current number. Thus, we can scan the array twice. First, we calcuate the running product of the part before the current number. Second, we calculate the running product of the part after the current number through scanning from the end of the array.

``````public class Solution {
public int[] productExceptSelf(int[] nums) {
int leng = nums.length;
int[] ret = new int[leng];
if(leng == 0)
return ret;
int runningprefix = 1;
for(int i = 0; i < leng; i++){
ret[i] = runningprefix;
runningprefix*= nums[i];
}
int runningsufix = 1;
for(int i = leng -1; i >= 0; i--){
ret[i] *= runningsufix;
runningsufix *= nums[i];
}
return ret;

}
``````

}

• This idea is very smart..

• Leet code's performance measure is not consistent. The same solution gave me 50% as well.

• that's not the point dude! This idea is unbeatable...

• Brilliant solution....

• The naming of prefix and sufix really helped me to understand the thought process

• Is this really O(1) space?? Because we create a new array and the length of new array is the length of nums array, so isn't this O(n) space?