-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58af0db
commit ed87f38
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//** | ||
|
||
|
||
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. | ||
|
||
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. | ||
|
||
You must write an algorithm that runs in O(n) time and without using the division operation. | ||
|
||
|
||
|
||
Example 1: | ||
|
||
Input: nums = [1,2,3,4] | ||
Output: [24,12,8,6] | ||
Example 2: | ||
|
||
Input: nums = [-1,1,0,-3,3] | ||
Output: [0,0,9,0,0] | ||
|
||
|
||
**// | ||
|
||
|
||
|
||
class ProductArray { | ||
public int[] productExceptSelf(int[] nums) { | ||
|
||
int[] arr = new int[nums.length]; | ||
Arrays.fill(arr,1); | ||
int curr =1; | ||
for(int j=0;j<nums.length;j++){ | ||
arr[j]*=curr; | ||
curr*=nums[j]; | ||
} | ||
|
||
curr=1; | ||
for(int i=nums.length-1;i>-1;i--){ | ||
|
||
arr[i]*=curr; | ||
curr*=nums[i]; | ||
} | ||
return arr; | ||
|
||
} | ||
} |