image_2023-06-05_11-48-24.png
30.2 KB
Runtime2 msBeats50.58%
Memory51.1 MBBeats53.48%
#N238. Product of Array Except Self
problem link
#solution
Memory51.1 MBBeats53.48%
#N238. Product of Array Except Self
problem link
#solution
class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int arr[] = new int[n];
int left[] = new int[n];
arr[0] = left[n-1] = 1;
for(int i=1; i<n; i++){
arr[i] = arr[i-1]*nums[i-1];
}
for(int i=n-2; i>-1; i--){
left[i] = left[i+1]*nums[i+1];
}
for(int i=0; i<n; i++){
arr[i]*=left[i];
}
return arr;
}
}🔥2❤1