Skip to content

Commit 9f7f475

Browse files
authored
Merge pull request #872 from 0xff-dev/303
Add solution and test-cases for problem 303
2 parents c2e418d + 953c021 commit 9f7f475

File tree

3 files changed

+54
-29
lines changed

3 files changed

+54
-29
lines changed

leetcode/301-400/0303.Range-Sum-Query--Immutable/README.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [303.Range Sum Query - Immutable][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given an integer array `nums`, handle multiple queries of the following type:
75

8-
**Example 1:**
6+
1. Calculate the **sum** of the elements of `nums` between indices `left` and `right` **inclusive** where `left <= right`.
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
Implement the `NumArray` class:
149

15-
## 题意
16-
> ...
10+
- `NumArray(int[] nums)` Initializes the object with the integer array `nums`.
11+
- `int sumRange(int left, int right)` Returns the **sum** of the elements of `nums` between indices `left` and `right` **inclusive** (i.e. `nums[left] + nums[left + 1] + ... + nums[right]`).
1712

18-
## 题解
13+
**Example 1:**
1914

20-
### 思路1
21-
> ...
22-
Range Sum Query - Immutable
23-
```go
2415
```
25-
16+
Input
17+
["NumArray", "sumRange", "sumRange", "sumRange"]
18+
[[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]
19+
Output
20+
[null, 1, -1, -3]
21+
22+
Explanation
23+
NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);
24+
numArray.sumRange(0, 2); // return (-2) + 0 + 3 = 1
25+
numArray.sumRange(2, 5); // return 3 + (-5) + 2 + (-1) = -1
26+
numArray.sumRange(0, 5); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3
27+
```
2628

2729
## 结语
2830

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type NumArray struct {
4+
data []int
5+
}
6+
7+
func Constructor(nums []int) NumArray {
8+
for i := 1; i < len(nums); i++ {
9+
nums[i] += nums[i-1]
10+
}
11+
return NumArray{data: nums}
12+
}
13+
14+
func (this *NumArray) SumRange(left int, right int) int {
15+
del := 0
16+
if left > 0 {
17+
del = this.data[left-1]
18+
}
19+
return this.data[right] - del
20+
}
21+
22+
func Solution(nums []int, operations [][2]int) []int {
23+
ans := make([]int, len(operations))
24+
c := Constructor(nums)
25+
for i, op := range operations {
26+
ans[i] = c.SumRange(op[0], op[1])
27+
}
28+
return ans
529
}

leetcode/301-400/0303.Range-Sum-Query--Immutable/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
nums []int
14+
operations [][2]int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{-2, 0, 3, -5, 2, -1}, [][2]int{{0, 2}, {2, 5}, {0, 5}}, []int{1, -1, -3}},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.nums, c.operations)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.nums, c.operations)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)