-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy path0303-RangeSumQueryImmutable.cs
More file actions
39 lines (34 loc) · 1017 Bytes
/
0303-RangeSumQueryImmutable.cs
File metadata and controls
39 lines (34 loc) · 1017 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//-----------------------------------------------------------------------------
// Runtime: 140ms
// Memory Usage: 35.3 MB
// Link: https://leetcode.com/submissions/detail/352778066/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _0303_RangeSumQueryImmutable
{
private readonly int[] sums;
public _0303_RangeSumQueryImmutable(int[] nums)
{
sums = new int[nums.Length];
var sum = 0;
for (int i = 0; i < nums.Length; i++)
{
sum += nums[i];
sums[i] = sum;
}
}
public int SumRange(int i, int j)
{
if (i == 0)
return sums[j];
else
return sums[j] - sums[i - 1];
}
}
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.SumRange(i,j);
*/
}