Last active
January 4, 2016 20:56
-
-
Save Magdi/5999c2ad37916ca9fac1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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