当前位置:网站首页>力扣练习——40 区间和的个数
力扣练习——40 区间和的个数
2022-08-02 04:18:00 【qq_43403657】
40 区间和的个数
1.问题描述
给定一个整数数组 nums,返回区间和在 [lower, upper] 之间的个数,包含 lower 和 upper。
区间和 S(i, j) 表示在 nums 中,位置从 i 到 j 的元素之和,包含 i 和 j (i ≤ j)。
说明:
最直观的算法复杂度是 O(n2) ,请在此基础上优化你的算法。
示例:
输入: nums = [-2,5,-1], lower = -2, upper = 2,
输出: 3
解释: 3个区间分别是: [0,0], [2,2], [0,2],它们表示的和分别为: -2, -1, 2。
可使用以下main函数:
int main()
{
vector<int> nums;
int n,data,lower,upper;
cin>>n;
for(int j=0; j<n; j++)
{
cin>>data;
nums.push_back(data);
}
cin>>lower>>upper;
int res=Solution().countRangeSum(nums,lower,upper);
cout<<res<<endl;
return 0;
}
2.输入说明
首先输入nums数组的长度n,
然后输入n个整数,以空格分隔。
最后输入两个整数lower和upper。
1<=n<=10000
3.输出说明
输出一个整数
4.范例
输入
3
-2 5 -1
-2 2
输出
3
5.代码
#include <iostream>
#include <queue>
#include <cstdlib>
#include <cstring>
#include<set>
using namespace std;
int countRangeSum(vector<int>nums, int lower, int upper)
{
//参考题解:https://leetcode.cn/problems/count-of-range-sum/solution/327qu-jian-he-de-ge-shu-ti-jie-zong-he-by-xu-yuan-/
int len = nums.size();//数组长度
int presum = 0;//前缀和
multiset<int> S;//平衡树
S.insert(0);
int res = 0;//结果
for (int i = 0; i < len; i++) {
presum += nums[i];
//lower_bound返回数值第一个出现的位置
//upper_bound返回的是最后一个大于等于val的位置,也是有一个新元素val进来时的插入位置
res += distance(S.lower_bound(presum - upper), S.upper_bound(presum - lower));
//distance()计算两个迭代器表示的范围内包含元素的个数
S.insert(presum);
}
return res;
}
int main()
{
vector<int> nums;
int n, data, lower, upper;
cin >> n;
for (int j = 0; j < n; j++)
{
cin >> data;
nums.push_back(data);
}
cin >> lower >> upper;
int res = countRangeSum(nums, lower, upper);
cout << res << endl;
return 0;
}
边栏推荐
- nr部分计算
- Excel如何解密工作表保护
- A Practical Arrangement of Map GIS Development Matters (Part 1)
- Use the advanced timer of GD32F207 to generate hidden bugs in PWM waves
- lvm扩容(实战无废话)
- UI自动化测试框架搭建——标记性能较差用例
- C程序调试过程常见的错误
- batch_size of deep learning foundation
- How to decrypt worksheet protection in Excel
- AFMG SysTune1.3.7使用图解
猜你喜欢
MapFi paper structure organization
Deep Blue Academy-Visual SLAM Lecture 14-Chapter 6 Homework
OpenPCDet environment configuration of 3 d object detection and demo test
【STM32】 ADC模数转换
Nuscenes数据集总结(下)
Unreal回放系统剖析(上)
Minecraft 1.18.1、1.18.2模组开发 23.3D动画盔甲制作
【数字IC手撕代码】Verilog固定优先级仲裁器|题目|原理|设计|仿真
ADSP21489数据手册表摘要
吴恩达机器学习系列课程笔记——第八章:神经网络:表述(Neural Networks: Representation)
随机推荐
Research Notes (6) Indoor Path Planning Method Based on Environment Perception
ROS visualization of 3D target detection
分布式系统的一致性与共识(1)-综述
A practice arrangement about map GIS (below) GIS practice of Redis
违约金过高”的认定依据
8月1日“海豹数藏”将全网首发民族英雄林则徐《四行行书》数字藏品!
吴恩达机器学习系列课程笔记——第九章:神经网络的学习(Neural Networks: Learning)
无主复制系统(3)-Quorum一致性的局限性
已更新 联通 电信 tiny模式
如果有些字段不想进行序列化怎么办?
Batch normalization (BN) based on deep learning
6个月测试经验,面试跳槽狮子大开口要18K,只会点点点,给我整无语了。。
Jetson Nano 2GB Developer Kit Installation Instructions
STM32 OLED显示屏
多数据中心操作和检测并发写入
Excel skills daquan
MySQL read-write separation mysql-proxy deployment
ADSP21489仿真:Failed to set breakpoint: Can‘t set breakpoints in the current state: Running
A Practical Arrangement of Map GIS Development Matters (Part 1)
Deep Learning Basics Overfitting, Underfitting Problems, and Regularization