Skip to content

Commit

Permalink
Added Product of array except self
Browse files Browse the repository at this point in the history
  • Loading branch information
puneeter committed Dec 30, 2024
1 parent c0981c0 commit 2ea8a58
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
22 changes: 22 additions & 0 deletions LeetCode/ProductOfArrayExceptSelf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution(object):
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
full_product = 1
num_zeroes = 0
for num in nums:
if num == 0 and num_zeroes < 1:
num_zeroes += 1
continue
full_product *= num
res = []
for num in nums:
if num_zeroes > 1 or (num_zeroes == 1 and num != 0):
res.append(0)
elif num == 0:
res.append(full_product)
else:
res.append(full_product/num)
return res
1 change: 1 addition & 0 deletions LeetCode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
| [Palindromic Substrings](PalindromicSubstrings.py) | :rocket: | `#recursion` `#dynamic-programming` `#memoization` | Python |
| [Pascal's Triangle II](PascalsTriangle2.py) | :heavy_check_mark: | `#recursion` | Python |
| [Pow(x,n)](Pow(x,n).py) | :heavy_check_mark: | `#recursion` `#memoization` | Python |
| [Product of array except self](ProductOfArrayExceptSelf.py) | :heavy_check_mark: | `#array` `#prefix-sum` | Python |
| [Reverse Integer](ReverseInteger.py) | :heavy_check_mark: | `#math` | Python |
| [Reverse Linked List](ReverseLinkedList.py) | :heavy_check_mark: | `#linkedlist` `#recursion` | Python |
| [Reverse String](ReverseString.py) | :heavy_check_mark: | `#recursion` | Python |
Expand Down

0 comments on commit 2ea8a58

Please sign in to comment.