Skip to content

Instantly share code, notes, and snippets.

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