Skip to content

Commit 1547a6b

Browse files
committed
feat: add solutions to lc problem: No.1588. Sum of All Odd Length
Subarrays
1 parent 834d9ee commit 1547a6b

File tree

6 files changed

+193
-15
lines changed

6 files changed

+193
-15
lines changed

solution/1500-1599/1588.Sum of All Odd Length Subarrays/README.md

+71-2
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,96 @@
5151
<li><code>1 &lt;= arr[i] &lt;= 1000</code></li>
5252
</ul>
5353

54-
5554
## 解法
5655

5756
<!-- 这里可写通用的实现逻辑 -->
5857

58+
“前缀和”实现。
59+
5960
<!-- tabs:start -->
6061

6162
### **Python3**
6263

6364
<!-- 这里可写当前语言的特殊实现逻辑 -->
6465

6566
```python
66-
67+
class Solution:
68+
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
69+
n = len(arr)
70+
presum = [0] * (n + 1)
71+
for i in range(n):
72+
presum[i + 1] = presum[i] + arr[i]
73+
74+
res = 0
75+
for i in range(n):
76+
for j in range(0, n, 2):
77+
if i + j < n:
78+
res += presum[i + j + 1] - presum[i]
79+
return res
6780
```
6881

6982
### **Java**
7083

7184
<!-- 这里可写当前语言的特殊实现逻辑 -->
7285

7386
```java
87+
class Solution {
88+
public int sumOddLengthSubarrays(int[] arr) {
89+
int n = arr.length;
90+
int[] presum = new int[n + 1];
91+
for (int i = 0; i < n; ++i) {
92+
presum[i + 1] = presum[i] + arr[i];
93+
}
94+
int res = 0;
95+
for (int i = 0; i < n; ++i) {
96+
for (int j = 0; i + j < n; j += 2) {
97+
res += presum[i + j + 1] - presum[i];
98+
}
99+
}
100+
return res;
101+
}
102+
}
103+
```
104+
105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
int sumOddLengthSubarrays(vector<int>& arr) {
111+
int n = arr.size();
112+
int presum[n + 1];
113+
for (int i = 0; i < n; ++i) presum[i + 1] = presum[i] + arr[i];
114+
int res = 0;
115+
for (int i = 0; i < n; ++i)
116+
{
117+
for (int j = 0; i + j < n; j += 2)
118+
{
119+
res += presum[i + j + 1] - presum[i];
120+
}
121+
}
122+
return res;
123+
}
124+
};
125+
```
74126
127+
### **Go**
128+
129+
```go
130+
func sumOddLengthSubarrays(arr []int) int {
131+
n := len(arr)
132+
presum := make([]int, n+1)
133+
for i := range arr {
134+
presum[i+1] = presum[i] + arr[i]
135+
}
136+
res := 0
137+
for i := 0; i < n; i++ {
138+
for j := 0; i+j < n; j += 2 {
139+
res += presum[i+j+1] - presum[i]
140+
}
141+
}
142+
return res
143+
}
75144
```
76145

77146
### **...**

solution/1500-1599/1588.Sum of All Odd Length Subarrays/README_EN.md

+69-2
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,88 @@ If we add all these together we get 1 + 4 + 2 + 5 + 3 + 7 + 11 + 10 + 15 = 58</p
5050
<li><code>1 &lt;= arr[i] &lt;= 1000</code></li>
5151
</ul>
5252

53-
5453
## Solutions
5554

5655
<!-- tabs:start -->
5756

5857
### **Python3**
5958

6059
```python
61-
60+
class Solution:
61+
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
62+
n = len(arr)
63+
presum = [0] * (n + 1)
64+
for i in range(n):
65+
presum[i + 1] = presum[i] + arr[i]
66+
67+
res = 0
68+
for i in range(n):
69+
for j in range(0, n, 2):
70+
if i + j < n:
71+
res += presum[i + j + 1] - presum[i]
72+
return res
6273
```
6374

6475
### **Java**
6576

6677
```java
78+
class Solution {
79+
public int sumOddLengthSubarrays(int[] arr) {
80+
int n = arr.length;
81+
int[] presum = new int[n + 1];
82+
for (int i = 0; i < n; ++i) {
83+
presum[i + 1] = presum[i] + arr[i];
84+
}
85+
int res = 0;
86+
for (int i = 0; i < n; ++i) {
87+
for (int j = 0; i + j < n; j += 2) {
88+
res += presum[i + j + 1] - presum[i];
89+
}
90+
}
91+
return res;
92+
}
93+
}
94+
```
95+
96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
int sumOddLengthSubarrays(vector<int>& arr) {
102+
int n = arr.size();
103+
int presum[n + 1];
104+
for (int i = 0; i < n; ++i) presum[i + 1] = presum[i] + arr[i];
105+
int res = 0;
106+
for (int i = 0; i < n; ++i)
107+
{
108+
for (int j = 0; i + j < n; j += 2)
109+
{
110+
res += presum[i + j + 1] - presum[i];
111+
}
112+
}
113+
return res;
114+
}
115+
};
116+
```
67117
118+
### **Go**
119+
120+
```go
121+
func sumOddLengthSubarrays(arr []int) int {
122+
n := len(arr)
123+
presum := make([]int, n+1)
124+
for i := range arr {
125+
presum[i+1] = presum[i] + arr[i]
126+
}
127+
res := 0
128+
for i := 0; i < n; i++ {
129+
for j := 0; i+j < n; j += 2 {
130+
res += presum[i+j+1] - presum[i]
131+
}
132+
}
133+
return res
134+
}
68135
```
69136

70137
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int sumOddLengthSubarrays(vector<int>& arr) {
4+
int n = arr.size();
5+
int presum[n + 1];
6+
for (int i = 0; i < n; ++i) presum[i + 1] = presum[i] + arr[i];
7+
int res = 0;
8+
for (int i = 0; i < n; ++i)
9+
{
10+
for (int j = 0; i + j < n; j += 2)
11+
{
12+
res += presum[i + j + 1] - presum[i];
13+
}
14+
}
15+
return res;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func sumOddLengthSubarrays(arr []int) int {
2+
n := len(arr)
3+
presum := make([]int, n+1)
4+
for i := range arr {
5+
presum[i+1] = presum[i] + arr[i]
6+
}
7+
res := 0
8+
for i := 0; i < n; i++ {
9+
for j := 0; i+j < n; j += 2 {
10+
res += presum[i+j+1] - presum[i]
11+
}
12+
}
13+
return res
14+
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
class Solution {
22
public int sumOddLengthSubarrays(int[] arr) {
3-
int[] sum = new int[arr.length];
4-
for (int i = 0; i < arr.length; i++) {
5-
sum[i] = (i != 0 ? sum[i - 1] : 0) + arr[i];
3+
int n = arr.length;
4+
int[] presum = new int[n + 1];
5+
for (int i = 0; i < n; ++i) {
6+
presum[i + 1] = presum[i] + arr[i];
67
}
7-
int ans = 0;
8-
// sum[b] - sum[a] 为 (a,b] 的和
9-
for (int i = 0; i < arr.length; i++) {
10-
ans += arr[i];
11-
for (int j = i + 2; j < arr.length; j += 2) {
12-
// [i, j]
13-
ans += sum[j] - sum[i] + arr[i];
8+
int res = 0;
9+
for (int i = 0; i < n; ++i) {
10+
for (int j = 0; i + j < n; j += 2) {
11+
res += presum[i + j + 1] - presum[i];
1412
}
1513
}
16-
return ans;
14+
return res;
1715
}
1816
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
3+
n = len(arr)
4+
presum = [0] * (n + 1)
5+
for i in range(n):
6+
presum[i + 1] = presum[i] + arr[i]
7+
8+
res = 0
9+
for i in range(n):
10+
for j in range(0, n, 2):
11+
if i + j < n:
12+
res += presum[i + j + 1] - presum[i]
13+
return res

0 commit comments

Comments
 (0)