[LeetCode]560. 和为K的子数组(前缀和)

题目

给定一个整数数组和一个整数?k,你需要找到该数组中和为?k?的连续的子数组的个数。

示例 1 :

输入:nums = [1,1,1], k = 2
输出: 2 , [1,1] 与 [1,1] 为两种不同的情况。
说明 :

数组的长度为 [1, 20,000]。
数组中元素的范围是 [-1000, 1000] ,且整数?k?的范围是?[-1e7, 1e7]。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/subarray-sum-equals-k
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解

1 维护前缀和sum[i]=a0+a1+a2+...+ai,由于本题边初始化前缀和,边计算答案,所以前缀和用一个变量维护即可。
2 维护HashMap,key为前缀和,val为该前缀和目前为止出现的次数,所以维护前缀和和更新HashMap要同步进行。要将前缀和=0初始化为出现1次,使得nums[0]+nums[1]...=k的这种情况包含进来。
3 a4+a5+a6=k 等价于sum[6]-sum[3]=k;所以每次知道当前位置为止的前缀和,cnt只要累加HashMap中sum-k出现的次数即可。

代码

class Solution {
    public int subarraySum(int[] nums, int k) {
        HashMap<Integer, Integer> occurTimeMap = new HashMap<>();// 存储和为key出现过几次
        int sum = 0;
        int cnt = 0;

        occurTimeMap.put(0, 1);//
        for (int num : nums) {
            sum += num;
            cnt += occurTimeMap.getOrDefault(sum - k, 0);
            occurTimeMap.put(sum, occurTimeMap.getOrDefault(sum, 0) + 1);// 更新occurTimeMap
        }
        return cnt;
    }
}

原文地址:https://www.cnblogs.com/coding-gaga/p/12294863.html

时间: 2024-08-30 16:46:32

[LeetCode]560. 和为K的子数组(前缀和)的相关文章

Leetcode 560.和为k的子数组

和为k的子数组 给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] 与 [1,1] 为两种不同的情况. 说明 : 数组的长度为 [1, 20,000]. 数组中元素的范围是 [-1000, 1000] ,且整数 k 的范围是 [-1e7, 1e7]. 思路 灵活使用map来解决问题 1 import java.util.HashMap; 2 import java.util

[Swift]LeetCode713. 乘积小于K的子数组 | Subarray Product Less Than K

Your are given an array of positive integers nums. Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is less than k. Example 1: Input: nums = [10, 5, 2, 6], k = 100 Output: 8 Explanation: The 8

[LeetCode] 898. Bitwise ORs of Subarrays 子数组按位或操作

We have an array?A?of non-negative integers. For every (contiguous) subarray?B =?[A[i], A[i+1], ..., A[j]]?(with?i <= j), we take the bitwise OR of all the elements in?B, obtaining a result?A[i] | A[i+1] | ... | A[j]. Return the number of possible?re

和为K的子数组-哈希表

class Solution { public int subarraySum(int[] nums, int k) { HashMap<Integer,Integer> h=new HashMap<>(); int sum=0; int count=0; h.put(0,1); for(int i=0;i<nums.length;i++){ sum+=nums[i]; if(h.containsKey(sum-k)){ count+=h.get(sum-k); } h.pu

LeetCode OJ:Maximum Subarray(子数组最大值)

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the largest sum = 6. 典型的DP问题,递推条件还是想了有点长时间,代码如下所示: 1

[LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和

Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead. Example: Input: s = 7, nums = [2,3,1,2,4,3] Output: 2 Explanation: the subarr

剑指offer(二十三,二十四,二十五)最小的k个数,连续子数组的最大和,链表中环的入口节点

23:最小的k个数 题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 简单题.... function GetLeastNumbers_Solution(input, k) { if(k>input.length) return []; let ans = []; input = input.sort(); //console.log(input.join("").slice(0,4).split

[Swift Weekly Contest 118]LeetCode974. 和可被 K 整除的子数组 | Subarray Sums Divisible by K

Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K. Example 1: Input: A = [4,5,0,-2,-3,1], K = 5 Output: 7 Explanation: There are 7 subarrays with a sum divisible by K = 5: [4, 5, 0, -2

Leetcode 643.子数组最大平均数I

子数组最大平均数I 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. 示例 1: 输入: [1,12,-5,-6,50,3], k = 4 输出: 12.75 解释: 最大平均数 (12-5-6+50)/4 = 51/4 = 12.75 注意: 1 <= k <= n <= 30,000. 所给数据范围 [-10,000,10,000]. 思路 本题拿到手的时候,第一个思路就是对每一个元素都去求其长度为k的连续子数组的平均数,最后比较返回最大平均数,这样的