Leetcode 560.和为k的子数组

和为k的子数组

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

示例 1 :

输入:nums = [1,1,1], k = 2

输出: 2 , [1,1] 与 [1,1] 为两种不同的情况。

说明 :

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

思路

灵活使用map来解决问题

 1 import java.util.HashMap;
 2 import java.util.Map;
 3
 4 class Solution {
 5     public int subarraySum(int[] nums, int k) {
 6         int sum = 0, result = 0;
 7         Map<Integer, Integer> preSum = new HashMap<>();
 8         preSum.put(0, 1);
 9
10         for (int i = 0; i < nums.length; i++) {
11             sum += nums[i];
12             if (preSum.containsKey(sum - k)) {
13                 result += preSum.get(sum - k);
14             }
15             preSum.put(sum, preSum.getOrDefault(sum, 0) + 1);
16         }
17
18         return result;
19     }
20 }

原文地址:https://www.cnblogs.com/kexinxin/p/10374003.html

时间: 2024-08-30 06:53:49

Leetcode 560.和为k的子数组的相关文章

hihocoder-1796-完美K倍子数组

hihocoder-1796-完美K倍子数组  #1796 : 完美K倍子数组 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 如果一个数组满足长度至少是2,并且其中任意两个不同的元素Ai和Aj (i ≠ j)其和Ai+Aj都是K的倍数,我们就称该数组是 完美K倍数组. 现在给定一个包含N个整数的数组A = [A1, A2, ... AN]以及一个整数K,请你找出A的最长的完美子数组B,输出B的长度. 如果这样的子数组不存在,输出-1. 输入 第一行包含两个整数N和

713. 乘积小于K的子数组

给定一个正整数数组 nums. 找出该数组内乘积小于 k 的连续的子数组的个数. 示例 1: 输入: nums = [10,5,2,6], k = 100 输出: 8 解释: 8个乘积小于100的子数组分别为: [10], [5], [2], [6], [10,5], [5,2], [2,6], [5,2,6]. 需要注意的是 [10,5,2] 并不是乘积小于100的子数组. 说明: 0 < nums.length <= 50000 0 < nums[i] < 1000 0 <

[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-eq

leecode第五百六十题(和为K的子数组)

class Solution { public: int subarraySum(vector<int>& nums, int k) { int cur = 0, res = 0; unordered_map<int, int> um; // 注意这里前缀和多了一个0,防止漏掉数组的前缀和刚好等于k的情况 um[0] = 1; for (int num : nums) { cur += num; res += um.find(cur - k) == um.end() ? 0

[LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k

Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead. Example 1: Given nums = [1, -1, 5, -2, 3], k = 3, return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is th

LeetCode 5248. 统计「优美子数组」

地址 https://www.acwing.com/solution/leetcode/content/5801/ 题目描述给你一个整数数组 nums 和一个整数 k. 如果某个子数组中恰好有 k 个奇数数字,我们就认为这个子数组是「优美子数组」. 请返回这个数组中「优美子数组」的数目. 示例 1: 输入:nums = [1,1,2,1,1], k = 3 输出:2 解释:包含 3 个奇数的子数组是 [1,1,2,1] 和 [1,2,1,1] . 示例 2: 输入:nums = [2,4,6],

[LeetCode] Shortest Unsorted Continuous Subarray 最短无序连续子数组

Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too. You need to find the shortest such subarray and output its length. E

LeetCode 53. 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. click to show more practice. Mor

【算法30】从数组中选择k组长度为m的子数组,要求其和最小

原题链接:codeforce 267 Div2 C 问题描述: 给定长度为n的数组a[],从中选择k个长度为m的子数组,要求和最大. 形式描述为:选择$k$个子数组[$l_1$, $r_1$], [$l_2$, $r_2$], ..., [$l_k$l1, $r_k$] (1 ≤ $l_1$ ≤$r_1$ ≤$l_2$ ≤ $r_2$ ≤... ≤$l_k$ ≤ $r_k$ ≤ n; $r_i-r_i+1$), 使得$\sum_{i=1}^{k}\sum_{j=l_i}^{r_i}p_j$ 问题