Skip to content

Commit a323e75

Browse files
committed
303
1 parent e8fe8bb commit a323e75

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,5 @@
247247
* [300. Longest Increasing Subsequence](leetcode-300-Longest-Increasing-Subsequence.md)
248248
* [301 题到 400 题](leetcode-301-400.md)
249249
* [301. Remove Invalid Parentheses](leetcode-301-Remove-Invalid-Parentheses.md)
250+
* [303. Range Sum Query - Immutable](leetcode-303-Range-Sum-Query-Immutable.md)
250251
* [更多](more.md)
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# 题目描述(简单难度)
2+
3+
303、Range Sum Query - Immutable
4+
5+
Given an integer array *nums*, find the sum of the elements between indices *i* and *j* (*i**j*), inclusive.
6+
7+
**Example:**
8+
9+
```
10+
Given nums = [-2, 0, 3, -5, 2, -1]
11+
12+
sumRange(0, 2) -> 1
13+
sumRange(2, 5) -> -1
14+
sumRange(0, 5) -> -3
15+
```
16+
17+
18+
19+
**Note:**
20+
21+
1. You may assume that the array does not change.
22+
2. There are many calls to *sumRange* function.
23+
24+
设计一个类,返回数组的第 `i``j` 个元素的和。
25+
26+
# 解法一 暴力
27+
28+
题目是想让我们设计一种数据结构,我们先暴力写一下。
29+
30+
```javascript
31+
/**
32+
* @param {number[]} nums
33+
*/
34+
var NumArray = function(nums) {
35+
this.nums = nums;
36+
};
37+
38+
/**
39+
* @param {number} i
40+
* @param {number} j
41+
* @return {number}
42+
*/
43+
NumArray.prototype.sumRange = function(i, j) {
44+
let sum = 0;
45+
for(; i <= j; i++){
46+
sum += this.nums[i];
47+
}
48+
return sum;
49+
};
50+
51+
/**
52+
* Your NumArray object will be instantiated and called as such:
53+
* var obj = new NumArray(nums)
54+
* var param_1 = obj.sumRange(i,j)
55+
*/
56+
```
57+
58+
分享 [官方](https://leetcode.com/problems/range-sum-query-immutable/solution/) 提供的一个优化,因为是多次调用 `sumRange`,我们可以在每次调用后将结果保存起来,这样的话第二次调用就可以直接返回了。
59+
60+
```java
61+
/**
62+
* @param {number[]} nums
63+
*/
64+
var NumArray = function(nums) {
65+
this.nums = nums;
66+
this.map = {};
67+
};
68+
69+
/**
70+
* @param {number} i
71+
* @param {number} j
72+
* @return {number}
73+
*/
74+
NumArray.prototype.sumRange = function(i, j) {
75+
let key = i + '@' + j;
76+
if(this.map.hasOwnProperty(key)){
77+
return this.map[key];
78+
}
79+
80+
let sum = 0;
81+
for(; i <= j; i++){
82+
sum += this.nums[i];
83+
}
84+
85+
this.map[key] = sum;
86+
return sum;
87+
};
88+
89+
/**
90+
* Your NumArray object will be instantiated and called as such:
91+
* var obj = new NumArray(nums)
92+
* var param_1 = obj.sumRange(i,j)
93+
*/
94+
```
95+
96+
# 解法二
97+
98+
[238 题](https://leetcode.wang/leetcode-238-Product-of-Array-Except-Self.html) 的解法二有一些像。
99+
100+
我们用一个数组保存累计的和,`numsAccumulate[i]` 存储 `0``i - 1` 累计的和。
101+
102+
如果我们想求 `i` 累积到 `j` 的和,只需要用 `numsAccumulate[j + 1]` 减去 `numsAccumulate[i]`
103+
104+
结合下边的图应该很好理解,我们要求的是橙色部分,相当于 `B` 的部分减去 `A` 的部分。
105+
106+
![](https://windliang.oss-cn-beijing.aliyuncs.com/303_2.jpg)
107+
108+
代码也就水到渠成了。
109+
110+
```javascript
111+
/**
112+
* @param {number[]} nums
113+
*/
114+
var NumArray = function (nums) {
115+
this.numsAccumulate = [0];
116+
let sum = 0;
117+
for (let i = 0; i < nums.length; i++) {
118+
sum += nums[i];
119+
this.numsAccumulate.push(sum);
120+
}
121+
};
122+
123+
/**
124+
* @param {number} i
125+
* @param {number} j
126+
* @return {number}
127+
*/
128+
NumArray.prototype.sumRange = function (i, j) {
129+
return this.numsAccumulate[j + 1] - this.numsAccumulate[i];
130+
};
131+
132+
/**
133+
* Your NumArray object will be instantiated and called as such:
134+
* var obj = new NumArray(nums)
135+
* var param_1 = obj.sumRange(i,j)
136+
*/
137+
```
138+
139+
#
140+
141+
比较简单的一道题,解法一做缓存的思路比较常见。解法二的思路印象中也遇到过几次,看到题目我的第一反应想到的就是解法二。

0 commit comments

Comments
 (0)