LeetCode 523. Continuous Subarray Sum

Problem Statement


use std::collections::HashMap;

impl Solution {
pub fn check_subarray_sum(nums: Vec<i32>, k: i32) -> bool {
let mut map: HashMap<i32, i32> = HashMap::new();
map.insert(0, -1);
let mut sum = 0;
for i in 0..nums.len() {
sum += nums[i];
let t = match k {
0 => sum,
_ => sum % k
};
if map.contains_key(&t) {
if i as i32 - *map.get(&t).unwrap() > 1 {
return true;
}
} else {
map.insert(t, i as i32);
}
}

return false;
}
}