Skip to content

Commit

Permalink
Create 0560-subarray-sum-equals-k.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
a93a committed Jan 28, 2023
1 parent 669079d commit c1eb794
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions kotlin/0560-subarray-sum-equals-k.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
fun subarraySum(nums: IntArray, k: Int): Int {
val hm = hashMapOf(0 to 1)
var res = 0
var sum = 0
nums.forEach {
sum += it
val prefix = sum - k
res += hm.getOrDefault(prefix, 0)
hm[sum] = hm.getOrDefault(sum, 0) + 1
}
return res
}
}

0 comments on commit c1eb794

Please sign in to comment.