Skip to content

Commit a694b91

Browse files
author
叽翅
authored
feat(leetcode): 1588 (#115)
1 parent a50c875 commit a694b91

File tree

1 file changed

+26
-0
lines changed
  • Codes/StarDrewer/1588_sum-of-all-odd-length-subarrays

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func sumOddLengthSubarrays(arr []int) (res int) {
8+
for i := 1; i <= len(arr); i += 2 {
9+
// 奇数子数组长度i
10+
11+
// 滑动窗口
12+
for j := 0; j+i <= len(arr); j++ {
13+
for k := j; k < j+i; k++ {
14+
res += arr[k]
15+
}
16+
}
17+
}
18+
return
19+
}
20+
21+
func main() {
22+
fmt.Println(sumOddLengthSubarrays([]int{1, 4, 2, 5, 3}))
23+
fmt.Println(sumOddLengthSubarrays([]int{1,2}))
24+
fmt.Println(sumOddLengthSubarrays([]int{10,11,12}))
25+
fmt.Println(sumOddLengthSubarrays([]int{}))
26+
}

0 commit comments

Comments
 (0)