当前位置:网站首页>leetcode-303:区域和检索 - 数组不可变
leetcode-303:区域和检索 - 数组不可变
2022-07-07 08:18:00 【菊头蝙蝠】
题目
给定一个整数数组 nums,处理以下类型的多个查询:
- 计算索引 left 和 right (包含 left 和 right)之间的 nums 元素的 和 ,其中 left <= right
实现 NumArray 类:
- NumArray(int[] nums) 使用数组 nums 初始化对象
- int sumRange(int i, int j) 返回数组 nums 中索引 left 和 right 之间的元素的 总和 ,包含 left 和 right 两点(也就是 nums[left] + nums[left + 1] + … + nums[right] )
示例 1:
输入:
["NumArray", "sumRange", "sumRange", "sumRange"]
[[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]
输出:
[null, 1, -1, -3]
解释:
NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);
numArray.sumRange(0, 2); // return 1 ((-2) + 0 + 3)
numArray.sumRange(2, 5); // return -1 (3 + (-5) + 2 + (-1))
numArray.sumRange(0, 5); // return -3 ((-2) + 0 + 3 + (-5) + 2 + (-1))
解题
方法一:前缀和
s[i]=nums[0]+nums[1]+....+nums[i-1];
如果要求 nums[2]+nums[3]+nums[4] 只需要
求s[4]-s[1]即可。
因此可以在初始化的时候,就去计算出每个s[i]的值
class NumArray {
public:
vector<int> sums;
NumArray(vector<int>& nums) {
int n=nums.size();
sums.resize(n+1);
for(int i=1;i<=nums.size();i++){
sums[i]=sums[i-1]+nums[i-1];
}
}
int sumRange(int left, int right) {
return sums[right+1]-sums[left];
}
};
边栏推荐
- The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file
- . Net configuration system
- STM32基础知识—内存映射
- Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
- Methods of adding centerlines and centerlines in SolidWorks drawings
- 5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!
- .NET配置系统
- BigDecimal value comparison
- [second on] [jeecgboot] modify paging parameters
- 求最大公约数与最小公倍数(C语言)
猜你喜欢

对word2vec的一些浅层理解

The method of word automatically generating directory

Vs code specifies the extension installation location

Review of the losers in the postgraduate entrance examination

Postman interface test III

ORM -- logical relation and & or; Sort operation, update record operation, delete record operation

Embedded background - chip

How to cancel automatic saving of changes in sqlyog database

Programming features of ISP, IAP, ICP, JTAG and SWD

字符串格式化
随机推荐
Mongodb creates an implicit database as an exercise
对存储过程进行加密和解密(SQL 2008/SQL 2012)
Appx代碼簽名指南
Programming features of ISP, IAP, ICP, JTAG and SWD
ArcGIS operation: batch modify attribute table
ORM -- database addition, deletion, modification and query operation logic
Experience sharing of software designers preparing for exams
IPv4 socket address structure
【acwing】789. 数的范围(二分基础)
Vs code specifies the extension installation location
Study summary of postgraduate entrance examination in October
Kotlin实现微信界面切换(Fragment练习)
ISP、IAP、ICP、JTAG、SWD的编程特点
CONDA creates virtual environment offline
2022.7.5DAY597
P1223 排队接水/1319:【例6.1】排队接水
[email protected]能帮助我们快速拿到日志对象
Methods of adding centerlines and centerlines in SolidWorks drawings
【acwing】786. Number k
When there are pointer variable members in the custom type, the return value and parameters of the assignment operator overload must be reference types