Skip to content

Commit 2aead21

Browse files
committed
🔥 Product of Array Except Self
1 parent bebbd0e commit 2aead21

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ Solutions of LeetCode Blind 75 Problems in JavaScript
44

55
:goal_net: [Curated List of Top 75 LeetCode Questions](https://www.teamblind.com/post/New-Year-Gift---Curated-List-of-Top-75-LeetCode-Questions-to-Save-Your-Time-OaM1orEU)
66

7-
| Problem | Difficulty | Tags | LeetCode |
8-
| ----------------------------------------------------------------------- | ------------------------------------------------------ | --------------------------------------- | ------------------------------------------------------------------------ |
9-
| [Two Sum](./two-sum.js) | <img src="https://img.shields.io/badge/-Easy-green" /> | `Array`, `Hash Table` | [:link:](https://leetcode.com/problems/two-sum/) |
10-
| [Best Time to Buy and Sell Stock](./best-time-to-buy-and-sell-stock.js) | <img src="https://img.shields.io/badge/-Easy-green" /> | `Array`, `Dynamic Programming` | [:link:](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) |
11-
| [Contains Duplicate](./contains-duplicate.js) | <img src="https://img.shields.io/badge/-Easy-green" /> | `Array`, `Set`, `Sorting`, `Hash Table` | [:link:](https://leetcode.com/problems/contains-duplicate/) |
7+
| Problem | Difficulty | Tags | LeetCode |
8+
| ----------------------------------------------------------------------- | --------------------------------------------------------- | --------------------------------------- | ------------------------------------------------------------------------ |
9+
| [Two Sum](./two-sum.js) | <img src="https://img.shields.io/badge/-Easy-green" /> | `Array`, `Hash Table` | [:link:](https://leetcode.com/problems/two-sum/) |
10+
| [Best Time to Buy and Sell Stock](./best-time-to-buy-and-sell-stock.js) | <img src="https://img.shields.io/badge/-Easy-green" /> | `Array`, `Dynamic Programming` | [:link:](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) |
11+
| [Contains Duplicate](./contains-duplicate.js) | <img src="https://img.shields.io/badge/-Easy-green" /> | `Array`, `Set`, `Sorting`, `Hash Table` | [:link:](https://leetcode.com/problems/contains-duplicate/) |
12+
| [Product of Array Except Self](./product-of-array-except-self.js) | <img src="https://img.shields.io/badge/-Medium-orange" /> | `Array` | [:link:](https://leetcode.com/problems/product-of-array-except-self/) |

product-of-array-except-self.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const productExceptSelf = (nums) => {
2+
const result = new Array(nums.length).fill(1);
3+
4+
let prefix = 1;
5+
for (let i = 0; i < nums.length; i++) {
6+
result[i] = prefix;
7+
prefix *= nums[i];
8+
}
9+
10+
let postfix = 1;
11+
for (let i = nums.length - 1; i >= 0; i--) {
12+
result[i] *= postfix;
13+
postfix *= nums[i];
14+
}
15+
16+
return result;
17+
};

0 commit comments

Comments
 (0)