当前位置:网站首页>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];
}
};
边栏推荐
- [email protected] can help us get the log object quickly
- Fiddler simulates the interface test
- leetcode-303:区域和检索 - 数组不可变
- Pdf document signature Guide
- 01 use function to approximate cosine function (15 points)
- Hdu-2196 tree DP learning notes
- STM32 product introduction
- P1031 [NOIP2002 提高组] 均分纸牌
- STM32 ADC和DMA
- [email protected]能帮助我们快速拿到日志对象
猜你喜欢
openinstall与虎扑达成合作,挖掘体育文化产业数据价值
STM32 ADC and DMA
Programming features of ISP, IAP, ICP, JTAG and SWD
Use the fetch statement to obtain the repetition of the last row of cursor data
Postman interface test III
使用U2-Net深层网络实现——证件照生成程序
[higherhrnet] higherhrnet detailed heat map regression code of higherhrnet
This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
LeetCode 练习——113. 路径总和 II
Several schemes of building hardware communication technology of Internet of things
随机推荐
Study summary of postgraduate entrance examination in July
Factorial implementation of large integer classes
Appx code signing Guide
How embedded engineers improve work efficiency
[牛客网刷题 Day5] JZ77 按之字形顺序打印二叉树
1323:【例6.5】活动选择
fiddler-AutoResponder
01 use function to approximate cosine function (15 points)
基于gis三维可视化技术的智慧城市建设
UnityWebRequest基础使用之下载文本、图片、AB包
How to cancel automatic saving of changes in sqlyog database
JMeter installation
Pdf document signature Guide
Can I open a stock trading account online? Is it safe
LeetCode 练习——113. 路径总和 II
HDU-2196 树形DP学习笔记
1324:【例6.6】整数区间
Study summary of postgraduate entrance examination in September
ORM -- database addition, deletion, modification and query operation logic
基于HPC场景的集群任务调度系统LSF/SGE/Slurm/PBS