【每日一题0927】子串求和

:mage:t2:给定一个数字组成的列表nums,请找出其中满足和值等于n的连续子串的个数。

示例:
输入: nums= [1, 2, 3, -3, -2, -1], n = 0,输出: 3
解析:满足和值等于0的子串有3个: [3, -3], [2, 3, -3, -2], [1, 2, 3, -3, -2, -1]

题目难度:中等
题目来源:codewars

def subsequence_sums(nums: list, n: int) -> int:
    pass

assert subsequence_sums([1, 2, 3, -3, -2, -1], 0) == 3
assert subsequence_sums([1, 5, -2, 4, 0, -7, -3, 6], 4) == 4
assert subsequence_sums([9, -2, -5, 8, 6, -10, 0, -4], -1) == 2
def subsequence_sums(nums: list, n: int) -> int:
    list1 = []
    sums = 0
    for i in range(len(nums)):
        for j in range(i+1,len(nums)+1):
            list1.append(nums[i:j])
    for i in list1:
        if sum(i) == n and sum(i[:len(i)//2])+sum(i[len(i)//2:]) == n:
            sums += 1
    return sums
def subsequence_sums(nums: list, n: int) -> int:
    count = 0
    temp_sum = 0
    for x in range(len(nums)):
        for y in nums[x:]:
            temp_sum += y
            if temp_sum == n:
                count += 1
                continue
        temp_sum = 0
    return count


assert subsequence_sums([1, 2, 3, -3, -2, -1], 0) == 3
assert subsequence_sums([1, 5, -2, 4, 0, -7, -3, 6], 4) == 4
assert subsequence_sums([9, -2, -5, 8, 6, -10, 0, -4], -1) == 2
1 Like
def subsequence_sums(nums: list, n: int) -> int:
    count=0
    list_sum=0
    for i in range(len(nums)):
        for j in nums[i:]:
            list_sum=j+list_sum
            if list_sum == n:
                count += 1
    return count
 public Integer subsequenceSums(List<Integer> arrayList, Integer integer) {
        int sumTemp =0;
        int res =0;
        for (int i = 0; i < arrayList.size(); i++) {
            for (int j = i; j < arrayList.size(); j++) {
                sumTemp = sumTemp + arrayList.get(j);
                if (sumTemp == integer){
                    res ++;
                }
            }
            sumTemp = 0;
        }
        return res;
    }

    @Test
    public void testSubsequenceSums(){
        assert subsequenceSums(Arrays.asList(1, 2, 3, -3, -2, -1), 0).equals(3);
        assert subsequenceSums(Arrays.asList(1, 5, -2, 4, 0, -7, -3, 6), 4).equals(4);
        assert subsequenceSums(Arrays.asList(9, -2, -5, 8, 6, -10, 0, -4), -1).equals(2);
    }
"""
__author__ = '晨&宇'
__time__ = '2021/9/28 20:32'
给定一个数字组成的列表nums,请找出其中满足和值等于n的连续子串的个数。
示例:
输入: nums= [1, 2, 3, -3, -2, -1], n = 0,输出: 3。
解析:满足和值等于0的子串有3个: [3, -3], [2, 3, -3, -2], [1, 2, 3, -3, -2, -1]
题目难度:中等
题目来源:codewars 3
"""


def subsequence_sums(nums: list, n: int) -> int:
    list_a = []
    result = []
    for i in range(len(nums) - 1):
        for j in range(1, len(nums) + 1):
            s = 0
            for x in nums[i:j]:
                s += x
            if s == n:
                list_a.append(nums[i:j])
    for y in range(len(list_a)):
        if len(list_a[y]) != 0:
            result.append(list_a[y])
    return len(result)


assert subsequence_sums([1, 2, 3, -3, -2, -1], 0) == 3
assert subsequence_sums([1, 5, -2, 4, 0, -7, -3, 6], 4) == 4
assert subsequence_sums([9, -2, -5, 8, 6, -10, 0, -4], -1) == 2