Skip to content

Commit 2568bee

Browse files
committed
Add solution for 0523.Continuous Subarray Sum
1 parent 6233483 commit 2568bee

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Continuous Subarray Sum
2+
3+
Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to a multiple of k, that is, sums up to n*k where n is also an integer.
4+
5+
## Example 1:
6+
```
7+
Input: [23, 2, 4, 6, 7], k=6
8+
Output: True
9+
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.
10+
```
11+
12+
## Example 2:
13+
```
14+
Input: [23, 2, 6, 4, 7], k=6
15+
Output: True
16+
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.
17+
```
18+
19+
20+
21+
## Note:
22+
23+
1. The length of the array won't exceed 10,000.
24+
2. You may assume the sum of all the numbers is in the range of a signed 32-bit integer.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public boolean checkSubarraySum(int[] nums, int k) {
3+
for (int start = 0; start < nums.length; start++) {
4+
int check = 0;
5+
for (int i = start; i < nums.length; i++) {
6+
check += nums[i];
7+
if (i > start) {
8+
if (k != 0) {
9+
if (check % k == 0) {
10+
return true;
11+
}
12+
} else {
13+
if (check == k) {
14+
return true;
15+
}
16+
}
17+
}
18+
}
19+
}
20+
21+
return false;
22+
}
23+
}

0 commit comments

Comments
 (0)