Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7f633e4

Browse files
committed
🐱(dynamic): 303. 区域和检索 - 数组不可变
补充 Golang 解法
1 parent e7a3d3d commit 7f633e4

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

docs/algorithm/dynamic/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,10 @@ class Solution:
10651065

10661066
动态规划前缀和。
10671067

1068+
<!-- tabs:start -->
1069+
1070+
#### **Python**
1071+
10681072
```python
10691073
class NumArray:
10701074

@@ -1090,6 +1094,46 @@ class NumArray:
10901094
# param_1 = obj.sumRange(i,j)
10911095
```
10921096

1097+
#### **Go**
1098+
1099+
```go
1100+
type NumArray struct {
1101+
sums []int
1102+
}
1103+
1104+
1105+
func Constructor(nums []int) NumArray {
1106+
length := len(nums)
1107+
sums := make([]int, length)
1108+
for i, num := range nums {
1109+
if i == 0 {
1110+
sums[i] = num
1111+
} else {
1112+
sums[i] = sums[i - 1] + num
1113+
}
1114+
}
1115+
return NumArray{sums}
1116+
}
1117+
1118+
1119+
func (this *NumArray) SumRange(i int, j int) int {
1120+
if i == 0 {
1121+
return this.sums[j]
1122+
} else {
1123+
return this.sums[j] - this.sums[i - 1]
1124+
}
1125+
}
1126+
1127+
1128+
/**
1129+
* Your NumArray object will be instantiated and called as such:
1130+
* obj := Constructor(nums);
1131+
* param_1 := obj.SumRange(i,j);
1132+
*/
1133+
```
1134+
1135+
<!-- tabs:end -->
1136+
10931137
## 309. 最佳买卖股票时机含冷冻期
10941138

10951139
[原题链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/)

0 commit comments

Comments
 (0)