Skip to content

Commit ed5024e

Browse files
Merge pull request #2957 from master-of-none/problem-523
Create: 0523-continuous-subarray-sum.rs
2 parents 2bc2e42 + 1fee4ce commit ed5024e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

rust/0523-continuous-subarray-sum.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn check_subarray_sum(nums: Vec<i32>, k: i32) -> bool {
5+
let mut hashmap: HashMap<i32, i32> = HashMap::new();
6+
hashmap.insert(0, -1);
7+
let mut sum = 0;
8+
9+
for (i, &num) in nums.iter().enumerate() {
10+
sum += num;
11+
if let Some(&prev_index) = hashmap.get(&(sum % k)) {
12+
if i as i32 - prev_index >= 2 {
13+
return true;
14+
}
15+
} else {
16+
hashmap.insert(sum % k, i as i32);
17+
}
18+
}
19+
20+
false
21+
}
22+
}

0 commit comments

Comments
 (0)