diff --git a/leetcode/301-400/0303.Range-Sum-Query--Immutable/README.md b/leetcode/301-400/0303.Range-Sum-Query--Immutable/README.md index 1f2a9fed8..be1cef3d6 100644 --- a/leetcode/301-400/0303.Range-Sum-Query--Immutable/README.md +++ b/leetcode/301-400/0303.Range-Sum-Query--Immutable/README.md @@ -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 +``` ## 结语 diff --git a/leetcode/301-400/0303.Range-Sum-Query--Immutable/Solution.go b/leetcode/301-400/0303.Range-Sum-Query--Immutable/Solution.go index d115ccf5e..9311e0d36 100644 --- a/leetcode/301-400/0303.Range-Sum-Query--Immutable/Solution.go +++ b/leetcode/301-400/0303.Range-Sum-Query--Immutable/Solution.go @@ -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 } diff --git a/leetcode/301-400/0303.Range-Sum-Query--Immutable/Solution_test.go b/leetcode/301-400/0303.Range-Sum-Query--Immutable/Solution_test.go index 14ff50eb4..4ed7195be 100644 --- a/leetcode/301-400/0303.Range-Sum-Query--Immutable/Solution_test.go +++ b/leetcode/301-400/0303.Range-Sum-Query--Immutable/Solution_test.go @@ -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() { }