560. Subarray Sum Equals K

package LeetCode_560

/**
 * 560. Subarray Sum Equals K
 * https://leetcode.com/problems/subarray-sum-equals-k/description/
 *
 * Given an array of integers and an integer k,
 * you need to find the total number of continuous subarrays whose sum equals to k.
Example 1:
Input:nums = [1,1,1], k = 2
Output: 2
 * */
class Solution {
    fun subarraySum(nums: IntArray, k: Int): Int {
        var result = 0
        val n = nums.size
        //method 1: O(n*n)
        /*for (i in 0 until n) {
            var num = nums[i]
            if (num == k) {
                result++
            }
            for (j in i + 1 until n) {
                num += nums[j]
                if (num == k) {
                    result++
                }
            }
        }*/

        //method 2: Time Complexity: O(n), Space Complexity: O(n)
        val map = HashMap<Int, Int>()
        //key:prefixSum value
        //value:number of occurence of the prefixSum value。
        map.put(0,1)
        var prefixSum = 0
        for (num in nums) {
            prefixSum += num
            result += map.getOrDefault(prefixSum - k, 0)
            map.put(prefixSum, map.getOrDefault(prefixSum, 0) + 1)
        }
        return result
    }
}

 

posted @ 2020-04-25 14:50  johnny_zhao  阅读(71)  评论(0编辑  收藏  举报