Skip to content

Instantly share code, notes, and snippets.

@Magdi
Last active January 4, 2016 20:56
Show Gist options
  • Save Magdi/5999c2ad37916ca9fac1 to your computer and use it in GitHub Desktop.
Save Magdi/5999c2ad37916ca9fac1 to your computer and use it in GitHub Desktop.
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] pre = new int[n];
for(int i = 0 ; i < n ; i++){
pre[i] = (i == 0) ? 1 : pre[i-1] * nums[i - 1];
}
int post = 1 ;
int[] res = new int[n];
for(int i = n-1 ; i >= 0 ; i--){
res[i] = post * pre[i];
post *= nums[i];
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment