任何人都可以解释下面的 if 语句吗
if (dict.contains(sum-k)) count+= dict(sum-k)
dict(sum) += 1
实际代码:
object Solution {
def subarraySum(nums: Array[Int], k: Int): Int = {
if (nums isEmpty) return 0
val dict = scala.collection.mutable.Map[Int, Int]().withDefaultValue(0)
var count = 0
nums.scanLeft(0)(_+_).foreach { sum =>
**if (dict.contains(sum-k)) count+= dict(sum-k)
dict(sum) += 1**
}
count
}
}