Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9ca4b0a

Browse files
solves product of array except itself in java
1 parent 180ce56 commit 9ca4b0a

File tree

2 files changed

+12
-21
lines changed

2 files changed

+12
-21
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@
192192
| 235 | [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree) | [![Java](assets/java.png)](src/LowestCommonAncestorOfBinarySearchTree.java) [![Python](assets/python.png)](python/lowest_common_ancestor_of_bst.py) | |
193193
| 236 | [Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree) | [![Java](assets/java.png)](src/LowestCommonAncestorOfBinaryTree.java) | |
194194
| 237 | [Delete a Node In A Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list) | [![Java](assets/java.png)](src/DeleteANodeInLinkedList.java) [![Python](assets/python.png)](python/delete_node_in_linked_list.py) | |
195-
| 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self) | | |
195+
| 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self) | [![Java](assets/java.png)](src/ProductOfArrayExceptItself.java) | |
196196
| 240 | [Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii) | | |
197197
| 241 | [Different Ways to Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses) | | |
198198
| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram) | [![Java](assets/java.png)](src/ValidAnagram.java) [![Python](assets/python.png)](python/delete_node_in_linked_list.py) | |

‎src/ProductOfArrayExceptItself.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,25 @@
44

55
public class ProductOfArrayExceptItself {
66

7-
private record ProductDetails(int nonZeroProduct, int numberOfZeros) { }
8-
97
public int[] productExceptSelf(int[] nums) {
10-
final ProductDetails productDetails = getProductDetails(nums);
11-
if (productDetails.numberOfZeros > 1) return new int[nums.length];
12-
if (productDetails.numberOfZeros == 1) return productSumOnlyWhenZero(nums, productDetails.nonZeroProduct);
138
final int[] result = new int[nums.length];
14-
for (int i = 0 ; i < result.length ; i++) {
15-
result[i] = productDetails.nonZeroProduct / nums[i];
16-
}
9+
multipleWithLeftPrefix(nums, result);
10+
multiplyWithRightPostfix(nums, result);
1711
return result;
1812
}
1913

20-
private int[] productSumOnlyWhenZero(int[] array, intproduct) {
21-
finalint[] result = newint[array.length];
22-
for(int i = 0 ; i < array.length ; i++) {
23-
if (array[i] == 0) result[i] = product;
14+
private voidmultipleWithLeftPrefix(int[] numbers, int[] result) {
15+
result[0] = 1;
16+
for(int i = 1 ; i < numbers.length ; i++) {
17+
result[i] =result[i - 1] * numbers[i - 1];
2418
}
25-
return result;
2619
}
2720

28-
private ProductDetails getProductDetails(int[] array) {
29-
int numberOfZeros = 0;
30-
int nonZeroProduct = 1;
31-
for (int element : array) {
32-
if (element == 0) numberOfZeros++;
33-
else nonZeroProduct *= element;
21+
private void multiplyWithRightPostfix(int[] numbers, int[] result) {
22+
int right = 1;
23+
for(int i = result.length - 1 ; i >= 0 ; i--) {
24+
result[i] *= right;
25+
right *= numbers[i];
3426
}
35-
return new ProductDetails(nonZeroProduct, numberOfZeros);
3627
}
3728
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /