Skip to content

Add solution and test-cases for problem 303 #872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions leetcode/301-400/0303.Range-Sum-Query--Immutable/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
# [303.Range Sum Query - Immutable][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
Given an integer array `nums`, handle multiple queries of the following type:

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

```
Input: a = "11", b = "1"
Output: "100"
```
Implement the `NumArray` class:

## 题意
> ...
- `NumArray(int[] nums)` Initializes the object with the integer array `nums`.
- `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]`).

## 题解
**Example 1:**

### 思路1
> ...
Range Sum Query - Immutable
```go
```

Input
["NumArray", "sumRange", "sumRange", "sumRange"]
[[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]
Output
[null, 1, -1, -3]

Explanation
NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);
numArray.sumRange(0, 2); // return (-2) + 0 + 3 = 1
numArray.sumRange(2, 5); // return 3 + (-5) + 2 + (-1) = -1
numArray.sumRange(0, 5); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3
```

## 结语

Expand Down
28 changes: 26 additions & 2 deletions leetcode/301-400/0303.Range-Sum-Query--Immutable/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
package Solution

func Solution(x bool) bool {
return x
type NumArray struct {
data []int
}

func Constructor(nums []int) NumArray {
for i := 1; i < len(nums); i++ {
nums[i] += nums[i-1]
}
return NumArray{data: nums}
}

func (this *NumArray) SumRange(left int, right int) int {
del := 0
if left > 0 {
del = this.data[left-1]
}
return this.data[right] - del
}

func Solution(nums []int, operations [][2]int) []int {
ans := make([]int, len(operations))
c := Constructor(nums)
for i, op := range operations {
ans[i] = c.SumRange(op[0], op[1])
}
return ans
}
21 changes: 10 additions & 11 deletions leetcode/301-400/0303.Range-Sum-Query--Immutable/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,30 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
nums []int
operations [][2]int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{-2, 0, 3, -5, 2, -1}, [][2]int{{0, 2}, {2, 5}, {0, 5}}, []int{1, -1, -3}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.nums, c.operations)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.nums, c.operations)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}