当前位置:网站首页>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];
}
};
边栏推荐
- XML configuration file parsing and modeling
- 求方程ax^2+bx+c=0的根(C语言)
- PDF文档签名指南
- Appx代码签名指南
- Experience sharing of software designers preparing for exams
- Multisim--软件相关使用技巧
- [second on] [jeecgboot] modify paging parameters
- Learning records - high precision addition and multiplication
- 0x0fa23729 (vcruntime140d.dll) (in classes and objects - encapsulation.Exe) exception thrown (resolved)
- Prototype object in ES6
猜你喜欢
随机推荐
The mobile terminal automatically adjusts the page content and font size by setting rem
嵌入式工程师如何提高工作效率
XML configuration file parsing and modeling
【剑指Offer】42. 栈的压入、弹出序列
Appx代码签名指南
The Hal library is configured with a general timer Tim to trigger ADC sampling, and then DMA is moved to the memory space.
求最大公约数与最小公倍数(C语言)
Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
【acwing】786. Number k
UnityWebRequest基础使用之下载文本、图片、AB包
HAL库配置通用定时器TIM触发ADC采样,然后DMA搬运到内存空间。
【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法
Sword finger offer 38 Arrangement of strings [no description written]
Factorial implementation of large integer classes
Postman interface test VII
Mongodb creates an implicit database as an exercise
移动端通过设置rem使页面内容及字体大小自动调整
fiddler-AutoResponder
Study summary of postgraduate entrance examination in August
The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file








