Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit72a77c9

Browse files
authored
Create Sum of Variable Length Subarrays.java
1 parente8baf50 commit72a77c9

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
classSolution {
2+
publicintsubarraySum(int[]nums) {
3+
intn =nums.length;
4+
int[]prefixSum =newint[n];
5+
intsum =0;
6+
intresult =0;
7+
for (inti =0;i <n;i++) {
8+
sum +=nums[i];
9+
prefixSum[i] =sum;
10+
intstart =Math.max(0,i -nums[i]);
11+
intsubarraySum =prefixSum[i];
12+
if (start >0) {
13+
subarraySum -=prefixSum[start -1];
14+
}
15+
result +=subarraySum;
16+
}
17+
returnresult;
18+
}
19+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp