560. Subarray Sum Equals K

Given an array of integers nums and an integer k, return the total number of continuous subarrays whose sum equals to k.

 

Example 1:

Input: nums = [1,1,1], k = 2
Output: 2

Example 2:

Input: nums = [1,2,3], k = 3
Output: 2

 

Constraints:

  • 1 <= nums.length <= 2 * 104
  • -1000 <= nums[i] <= 1000
  • -107 <= k <= 107

Rust Solution

struct Solution;

use std::collections::HashMap;

impl Solution {
    fn subarray_sum(nums: Vec<i32>, k: i32) -> i32 {
        let mut sum = 0;
        let mut hm: HashMap<i32, i32> = HashMap::new();
        let mut res = 0;
        hm.insert(0, 1);
        for x in nums {
            sum += x;
            res += hm.get(&(sum - k)).unwrap_or(&0);
            *hm.entry(sum).or_default() += 1;
        }
        res
    }
}

#[test]
fn test() {
    let nums = vec![1, 1, 1];
    let k = 2;
    assert_eq!(Solution::subarray_sum(nums, k), 2);
    let nums = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
    let k = 0;
    assert_eq!(Solution::subarray_sum(nums, k), 55);
}

Having problems with this solution? Click here to submit an issue on github.