Count of Range Sum
Hard
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.
Range sum S(i, j) is defined as the sum of the elements in nums between indices i and j (i ≤ j), inclusive.
Note:
A naive algorithm of O(n2) is trivial. You MUST do better than that.
Example:
Input: nums = [-2,5,-1], lower = -2, upper = 2,
Output: 3
Explanation: The three ranges are : [0,0], [2,2], [0,2] and their respective sums are: -2, -1, 2.
题意
给定一个数组nums和整数区间[lower, upper],求nums的连续子串的个数,使得连续子串的和在区间[lower, upper]内。
思路
将连续子串和转化为两个前缀和相减,即在n个前缀和中寻找(i, j)对,使得lower <= sum[i] - sum[j] <= upper && i > j. 用TreeMap保存之前已经遍历过的前缀和,Tr

这是一篇关于LeetCode 327题目的博客,主要讨论如何利用Java和TreeMap数据结构解决在给定数组中寻找满足特定范围条件的子数组和的问题。通过将子数组和转化为前缀和的差值,借助TreeMap以O(nlogn)的时间复杂度找出符合条件的子数组个数。
&spm=1001.2101.3001.5002&articleId=100932409&d=1&t=3&u=5234046c1c0547b698a165759f8f63f1)
380

被折叠的 条评论
为什么被折叠?



