Skip to content

Commit 6e52a5e

Browse files
committed
238.product_of_array_except_self
1 parent 1563611 commit 6e52a5e

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
|49|[Group Anagrams](./leetcode/0049.group_anagrams/README.md)|[Go](./leetcode/0049.group_anagrams/49.group_anagrams.go)|Medium|Completed|
1010
|121|[Best Time to Buy and Sell Stock](./leetcode/0121.best_time_to_buy_and_sell_stock/README.md)|[Go](./leetcode/0121.best_time_to_buy_and_sell_stock/121.best_time_to_buy_and_sell_stock.go)|Easy|Completed|
1111
|217|[Contains Duplicate](./leetcode/0217.contains_duplicate/README.md)|[Go](./leetcode/0217.contains_duplicate/217.contains_duplicate.go)|Easy|Completed|
12+
|238|[Product of Array Except Self](./leetcode/0238.product_of_array_except_self/README.md)|[Go](./leetcode/0238.product_of_array_except_self/238.product_of_array_except_self.go)|Medium|Completed|
1213
|242|[Valid Anagram](./leetcode/0242.valid_anagram/README.md)|[Go](./leetcode/0242.valid_anagram/242.valid_anagram.go)|Easy|Completed|
1314
|243|[Shortest Word Distance🔒](./leetcode/0243.shortest_word_distance/README.md)|[Go](./leetcode/0243.shortest_word_distance/243.shortest_word_distance.go)|Easy|Completed|
1415
|244|[Shortest Word Distance II🔒](./leetcode/0244.shortest_word_distance_2/README.md)|[Go](./leetcode/0244.shortest_word_distance_2/244_shortest_word_distance_2.go)|Medium|In-progress|
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package data_structures_algorithms
2+
3+
func productExceptSelf(nums []int) []int {
4+
length := len(nums)
5+
result := make([]int, length)
6+
result[0], result[length-1] = 1, 1
7+
8+
for i := 1; i < length; i++ {
9+
result[i] = result[i-1] * nums[i-1]
10+
}
11+
12+
postProduct := 1
13+
for i := length - 2; i >= 0; i-- {
14+
postProduct *= nums[i+1]
15+
result[i] *= postProduct
16+
}
17+
18+
return result
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 238. Product of Array Except Self
2+
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]`.
3+
4+
The product of any prefix or suffix of `nums` is guaranteed to fit in a 32-bit integer.
5+
6+
You must write an algorithm that runs in `O(n)` time and without using the division operation.
7+
8+
#### Example 1:
9+
```shell
10+
Input: nums = [1,2,3,4]
11+
Output: [24,12,8,6]
12+
```
13+
14+
#### Example 2:
15+
```shell
16+
Input: nums = [-1,1,0,-3,3]
17+
Output: [0,0,9,0,0]
18+
```
19+
20+
<br>
21+
22+
#### Constraints:
23+
- <code>2 <= nums.length <= 10<sup>5</sup></code>
24+
- `-30 <= nums[i] <= 30`
25+
- The product of any prefix or suffix of `nums` is guaranteed to fit in a 32-bit integer.
26+
27+
**Follow up**: Can you solve the problem using only `O(1)` space? The output array does no tcount as extra space for space complexity analysis

0 commit comments

Comments
 (0)