Skip to content

Commit 0dc4c02

Browse files
committed
Add solution #303
1 parent a6614d2 commit 0dc4c02

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@
226226
283|[Move Zeroes](./0283-move-zeroes.js)|Easy|
227227
290|[Word Pattern](./0290-word-pattern.js)|Easy|
228228
295|[Find Median from Data Stream](./0295-find-median-from-data-stream.js)|Hard|
229+
303|[Range Sum Query - Immutable](./0303-range-sum-query-immutable.js)|Easy|
229230
316|[Remove Duplicate Letters](./0316-remove-duplicate-letters.js)|Medium|
230231
322|[Coin Change](./0322-coin-change.js)|Medium|
231232
326|[Power of Three](./0326-power-of-three.js)|Easy|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 303. Range Sum Query - Immutable
3+
* https://leetcode.com/problems/range-sum-query-immutable/
4+
* Difficulty: Easy
5+
*
6+
* Given an integer array nums, handle multiple queries of the following type:
7+
* - Calculate the sum of the elements of nums between indices left and right inclusive
8+
* where left <= right.
9+
*
10+
* Implement the NumArray class:
11+
* - NumArray(int[] nums) Initializes the object with the integer array nums.
12+
* - int sumRange(int left, int right) Returns the sum of the elements of nums between
13+
* indices left and right inclusive (i.e. nums[left] + nums[left + 1] + ... + nums[right]).
14+
15+
*/
16+
17+
/**
18+
* @param {number[]} nums
19+
*/
20+
var NumArray = function(nums) {
21+
this.nums = nums;
22+
};
23+
24+
/**
25+
* @param {number} left
26+
* @param {number} right
27+
* @return {number}
28+
*/
29+
NumArray.prototype.sumRange = function(left, right) {
30+
return this.nums.slice(left, right + 1).reduce((sum, n) => sum + n, 0);
31+
};

0 commit comments

Comments
 (0)