GeezerVison is showing github.com — 238_Product_of_Array_Except_Self · dolphintear/leetcode-1@55a9b4c
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: README.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -87,6 +87,7 @@ Also, there are open source implementations for basic data structs and algorithm |
87 | 87 | | 220 | [Contains Duplicate III](https://leetcode.com/problems/contains-duplicate-iii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/220_Contains_Duplicate_III.py) | 1. Sort and binary Search <br>2. Bucket sort | |
88 | 88 | | 221 | [Maximal Square](https://leetcode.com/problems/maximal-square/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/221_Maximal_Square.py) | 1. Brute force<br> 2. dp[i][j] = min(dp[i-1][j], dp[i-1][j-1], dp[i][j-1]) + 1, O(mn) and O(mn)<br>3. dp[j] = min([j], dp[j-1], prev) + 1, O(mn) and O(n)| |
89 | 89 | | 228 | [Summary Ranges](https://leetcode.com/problems/summary-ranges/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/228_Summary_Ranges.py) | Detect start and jump, O(n) and O(1) | |
| 90 | +| 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/238_Product_of_Array_Except_Self.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/238_Product_of_Array_Except_Self.java) | The ans is [0,i -1] * [i+1, len- 1]. We can twice for left and right (reverse), O(n) and O(n) | |
90 | 91 | | 243 | [Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/243_Shortest_Word_Distance.py) | Update index1 and index2, and check distance, O(n) and O(1) | |
91 | 92 | | 246 | [Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/246_Strobogrammatic_Number.py) | Hash table and reverse string, O(n) and O(n) | |
92 | 93 | | 249 | [Group Shifted Strings](https://leetcode.com/problems/group-shifted-strings/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/249_Group_Shifted_Strings.py) | Hash and generate hash code for each string, O(n) and O(n) | |
|
Collapse file
Copy file name to clipboard +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +public class Solution { |
| 2 | + public int[] productExceptSelf(int[] nums) { |
| 3 | + int n = nums.length; |
| 4 | + int[] res = new int[n]; |
| 5 | + res[0] = 1; |
| 6 | + for (int i = 1; i < n; i++) { |
| 7 | + // left part |
| 8 | + res[i] = res[i - 1] * nums[i - 1]; |
| 9 | + } |
| 10 | + int right = 1; |
| 11 | + for (int i = n - 1; i >= 0; i--) { |
| 12 | + res[i] *= right; |
| 13 | + // right part |
| 14 | + right *= nums[i]; |
| 15 | + } |
| 16 | + return res; |
| 17 | + } |
| 18 | +} |
Collapse file
Copy file name to clipboard +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +class Solution(object): |
| 2 | + def productExceptSelf(self, nums): |
| 3 | + """ |
| 4 | + :type nums: List[int] |
| 5 | + :rtype: List[int] |
| 6 | + """ |
| 7 | + ans = [1] * len(nums) |
| 8 | + for i in range(1, len(nums)): |
| 9 | + ans[i] = ans[i - 1] * nums[i - 1] |
| 10 | + right = 1 |
| 11 | + for i in range(len(nums) - 1, -1, -1): |
| 12 | + ans[i] *= right |
| 13 | + right *= nums[i] |
| 14 | + return ans |