Skip to content

Commit 1fee4ce

Browse files
Create: 0523-continuous-subarray-sum.rs
1 parent 198f27b commit 1fee4ce

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)