Skip to content

Commit fced454

Browse files
committed
solve problem Product Of Array Except Self
1 parent 68f8c6e commit fced454

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ All solutions will be accepted!
298298
|513|[Find Bottom Left Tree Value](https://leetcode-cn.com/problems/find-bottom-left-tree-value/description/)|[java/py/js](./algorithms/FindBottomLeftTreeValue)|Medium|
299299
|103|[Binary Tree Zigzag Level Order Traversal](https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/description/)|[java/py/js](./algorithms/BinaryTreeZigzagLevelOrderTraversal)|Medium|
300300
|134|[Gas Station](https://leetcode-cn.com/problems/gas-station/description/)|[java/py/js](./algorithms/GasStation)|Medium|
301+
|238|[Product Of Array Except Self](https://leetcode-cn.com/problems/product-of-array-except-self/description/)|[java/py/js](./algorithms/ProductOfArrayExceptSelf)|Medium|
301302

302303
# Database
303304
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Product Of Array Except Self
2+
We can solve this problem by two array
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int[] productExceptSelf(int[] nums) {
3+
int length = nums.length;
4+
int[] reverses = new int[length];
5+
6+
for (int i = 0; i < length; i++)
7+
reverses[length - 1 - i] = i == 0 ? nums[length - 1 - i] : nums[length - 1 - i] * reverses[length - i];
8+
for (int i = 0; i < length; i++)
9+
nums[i] = i == 0 ? nums[i] : nums[i] * nums[i - 1];
10+
11+
reverses[0] = reverses[1];
12+
for (int i = 1; i < length - 1; i++)
13+
reverses[i] = nums[i - 1] * reverses[i + 1];
14+
reverses[length - 1] = nums[length - 2];
15+
16+
return reverses;
17+
}
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var productExceptSelf = function(nums) {
6+
let length = nums.length,
7+
reverses = new Array(length)
8+
9+
for (let i = 0; i < length; i++)
10+
reverses[length - 1 - i] = i == 0 ? nums[length - 1 - i] : nums[length - 1 - i] * reverses[length - i]
11+
for (let i = 0; i < length; i++)
12+
nums[i] = i == 0 ? nums[i] : nums[i] * nums[i - 1]
13+
14+
reverses[0] = reverses[1]
15+
for (let i = 1; i < length - 1; i++)
16+
reverses[i] = nums[i - 1] * reverses[i + 1]
17+
reverses[length - 1] = nums[length - 2]
18+
19+
return reverses
20+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def productExceptSelf(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: List[int]
6+
"""
7+
length = len(nums)
8+
reverse_products = [0] * length
9+
10+
for i in xrange(length):
11+
reverse_products[length - 1 - i] = nums[length - 1 - i] if i == 0 else nums[length - 1 - i] * reverse_products[length - i]
12+
for i in xrange(length):
13+
nums[i] = nums[i] if i == 0 else nums[i] * nums[i - 1]
14+
15+
reverse_products[0] = reverse_products[1]
16+
for i in xrange(1, length - 1):
17+
reverse_products[i] = nums[i - 1] * reverse_products[i + 1]
18+
reverse_products[-1] = nums[-2]
19+
20+
return reverse_products

0 commit comments

Comments
 (0)