当前位置:网站首页>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];
}
};
边栏推荐
猜你喜欢

嵌入式背景知识-芯片

Embedded background - chip

ArcGIS operation: converting DWG data to SHP data

JMeter loop controller and CSV data file settings are used together

LeetCode 练习——113. 路径总和 II

【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码

php \n 换行无法输出

Some properties of leetcode139 Yang Hui triangle

0x0fa23729 (vcruntime140d.dll) (in classes and objects - encapsulation.Exe) exception thrown (resolved)

IO模型复习
随机推荐
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
.NET配置系统
BigDecimal value comparison
The request object parses the request body and request header parameters
Guid primary key
每周推荐短视频:L2级有哪些我们日常中经常会用到的功能?
施努卡:机器人视觉抓取工作原理 机器视觉抓取
对存储过程进行加密和解密(SQL 2008/SQL 2012)
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
[detailed explanation of Huawei machine test] tall and short people queue up
【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
Yarn的基础介绍以及job的提交流程
Study summary of postgraduate entrance examination in September
Remote meter reading, switching on and off operation command
ORM -- grouping query, aggregation query, query set queryset object properties
Inno setup packaging and signing Guide
table宽度比tbody宽度大4px
IDA中常见快捷键
Sword finger offer 38 Arrangement of strings [no description written]
When there are pointer variable members in the custom type, the return value and parameters of the assignment operator overload must be reference types