当前位置:网站首页>Leetcode-303: region and retrieval - array immutable
Leetcode-303: region and retrieval - array immutable
2022-07-07 10:27:00 【Chrysanthemum headed bat】
leetcode-303: Area and retrieval - The array is immutable
subject
Given an array of integers nums, Handle multiple queries of the following types :
- Calculation index left and right ( contain left and right) Between nums Elemental and , among left <= right
Realization NumArray class :
- NumArray(int[] nums) Using arrays nums Initialize object
- int sumRange(int i, int j) Returns an array of nums Middle index left and right Between elements The sum of the , contain left and right At two o 'clock ( That is to say nums[left] + nums[left + 1] + … + nums[right] )
Example 1:
Input :
["NumArray", "sumRange", "sumRange", "sumRange"]
[[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]
Output :
[null, 1, -1, -3]
explain :
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))
Problem solving
Method 1 : The prefix and
s[i]=nums[0]+nums[1]+....+nums[i-1];
If required nums[2]+nums[3]+nums[4]
It only needs
seek s[4]-s[1]
that will do .
So you can initialize , Just calculate each s[i]
Value
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];
}
};
边栏推荐
- Fiddler simulates the interface test
- Inno setup packaging and signing Guide
- Programming features of ISP, IAP, ICP, JTAG and SWD
- 1321:【例6.3】删数问题(Noip1994)
- 1323:【例6.5】活动选择
- BigDecimal value comparison
- 【作业】2022.7.6 写一个自己的cal函数
- P1223 排队接水/1319:【例6.1】排队接水
- Word自动生成目录的方法
- Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
猜你喜欢
1323:【例6.5】活动选择
The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file
一文讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系
【acwing】786. 第k个数
原型与原型链
php \n 换行无法输出
SQLyog数据库怎么取消自动保存更改
【acwing】786. Number k
Methods of adding centerlines and centerlines in SolidWorks drawings
STM32 Basics - memory mapping
随机推荐
Easyexcel read write simple to use
Remote meter reading, switching on and off operation command
HAL库配置通用定时器TIM触发ADC采样,然后DMA搬运到内存空间。
Yarn的基础介绍以及job的提交流程
leetcode-303:区域和检索 - 数组不可变
[牛客网刷题 Day5] JZ77 按之字形顺序打印二叉树
[STM32] solution to the problem that SWD cannot recognize devices after STM32 burning program
How to cancel automatic saving of changes in sqlyog database
Leetcode exercise - 113 Path sum II
Pdf document signature Guide
2022.7.4DAY596
Guid primary key
Multisim--软件相关使用技巧
IIC基本知识
【acwing】786. 第k个数
JMeter about setting thread group and time
Factorial implementation of large integer classes
[sword finger offer] 42 Stack push in and pop-up sequence
串口通讯继电器-modbus通信上位机调试软件工具项目开发案例
The mobile terminal automatically adjusts the page content and font size by setting rem