题目:给定一组正整数数组A与一个正整数K,求整数数组A中和为K的子序列个数:
例如输入[1,1,1]
输出 2
思路:A[1]+A[2]+·····+A[j] = sub_Sum
代码:
def countSumK(A,K):
count = collections.Counter()
rs = 0
count[0] = 1
sub_Sum = 0
for i in range(len(A)):
sub_Sum += A[i]
rs += count[sub_Sum - K]
count[sub_Sum] +=1
return rs