力扣 hot 100 python

560. 和为 K 的子数组 - 力扣(LeetCode)

题目

给你一个整数数组 nums 和一个整数 k ,请你统计并返回 该数组中和为 k的子数组的个数。子数组是数组中元素的连续非空序列。

思路:暴力解,遍历每个子数组,固定起点i,j从 i 往后累加,每累加一次判断总和t是否等于 k,相等就计数 + 1。

复制代码
class Solution(object):
    def subarraySum(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """

        n = len(nums)
        res = 0
        
        if n == 0:
            return 0
        
        i = 0
        j = 0
        t = 0

        while i < n :
            j = i
            t = 0
            while j < n :
                t += nums[j]
                if t == k :
                    res += 1
                j += 1
            i += 1
        return res

时间复杂度 O (n²),大数据会超时

改进:

前缀和 + 哈希表思路:

子数组 nums j ... i‑1 的总和 = pre_sumi − pre_sumj 也就是:从下标 j 到 i‑1 这一段连续区间的和 = 两个前缀和相减。

我们希望这段子数组的和等于 k:

(pre_sumi - pre_sumj = k) 变形:

{pre_sumj = pre_sumi - k})

当我们遍历到位置 i,当前前缀和是 pre_sumi。 我想找:之前有没有出现过前缀和等于 pre_sumi‑k。

每出现过 1 次,就代表找到 1 个子数组,和为 k。

如果这个前缀和历史上出现过 3 次,那就一下子新增 3 个答案。

复制代码
class Solution(object):
    def subarraySum(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        from collections import defaultdict
        n = len(nums)
        res = 0
            
        if n == 0:
            return 0

        prefic_map = defaultdict(int)
        prefic_sum = 0
        prefic_map[0] = 1
        for i in range(n) :
            prefic_sum += nums[i]
            if (prefic_sum - k) in prefic_map :
                res += prefic_map[prefic_sum - k]
            prefic_map[prefic_sum] += 1

        return res
相关推荐
段一凡-华北理工大学5 小时前
工业智能体:从Codex/Claude到项目实战~系列文章05:从“AI助手“到“工业智能体“:能力跃迁与落地范式
人工智能·python·claude·codex·工业智能体·高炉炼铁智能化
深蓝AI5 小时前
ContextVar 到线程池就丢了?asyncio.to_thread 和 run_in_executor 实测
python
岁月宁静6 小时前
什么是 Agent:从"会聊天"到"会做事"
人工智能·python
岁月宁静6 小时前
AI 应用开发工程师简历怎么写:HR 初筛、技术复筛、面试追问,三道关卡各看什么?
人工智能·python·面试
来让爷抱一个7 小时前
Python 异步里混用同步库,事件循环被卡住的全过程
开发语言·python
小肝一下9 小时前
3.基础语法(3)
python
Dream it possible!9 小时前
LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)
算法·leetcode·面试·动态规划
心抵鹊9 小时前
算法之双指针系列(一)
算法·leetcode
十二9 小时前
【洛谷题解】P1083 [NOIP 2012 提高组] 借教室(二分答案 + 差分)
数据结构·c++·算法·蓝桥杯
深蓝AI9 小时前
Python 子进程树:父进程被杀后,孙进程为何还在跑?
python