/*
leetcode 303. Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
Example:
Given nums = [-2, 0, 3, -5, 2, -1]
sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3
Note:
You may assume that the array does not change.
There are many calls to sumRange function.
解题思路:
用一个数组dp保存nums的和,dp[i]表示nums[0...i]的和
求sumRange(i,j)即为dp[j]-dp[i-1];注意边界
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class NumArray {
private: vector<int> dp;
public:
NumArray(vector<int> &nums)
{
if (nums.size() == 0)
return;
dp.push_back(nums[0]);
for (int i = 1; i < nums.size(); ++i)
{
dp.push_back(dp[i - 1] + nums[i]);
//cout << dp[i] << endl;
}
}
int sumRange(int i, int j)
{
if (i == 0)
return dp[j];
return dp[j] - dp[i-1];
}
};
// Your NumArray object will be instantiated and called as such:
// NumArray numArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);
void test()
{
vector<int> nums{ -2, 0, 3, -5, 2, -1 };
NumArray numArray(nums);
cout << numArray.sumRange(0, 1) << endl;
cout << numArray.sumRange(1, 2) << endl;
}
int main()
{
test();
return 0;
}
相关推荐
流媒体弱网优化之路(NACK)——纯NACK方案的优化探索
流媒体学习之路(NACK)——纯NACK方案的最高性能 文章目录流媒体学习之路(NACK)——纯NACK方案的最高性能一、NACK简介二、Nack的理解2.1 一种补救策略2.2 什么时候要?2.3 要多少?2.4 怎么要?三、计算分析3.1 rtt强相关3.2 补包次数计算3.3 Nack的定时发送3.4 nack重传限制四、实验 近期在针对纯丢包场景做一些简单的探索实验,一个让我困扰的问题就是:不使用FEC而只是通过纯NACK进行补偿,到底能在实验环境下抵抗多少的丢包呢?每一次丢包补偿的最大时长
[LeetCode]303. Range Sum Query - Immutable
[LeetCode]303. Range Sum Query - Immutable题目描述思路开始的思路:存数组,将对应的区间内的数字相加 优化: 对于每一位,存入的是从第0位到当前位的和 返回的时候返回 [j] - [i - 1]即可代码开始的思路class NumArray { public: vector<int> nums; NumArray(vector<int>
从数据手册到代码:一步步解读SC7A20寄存器配置与加速度值换算原理
本文详细解析了SC7A20三轴加速度传感器的寄存器配置与加速度值换算原理。从数据手册出发,逐步拆解关键寄存器设置,并推导原始ADC值到物理加速度的完整换算过程,帮助开发者深入理解传感器底层工作原理,提升嵌入式开发效率。
LeetCode //C - 303. Range Sum Query - Immutable
【代码】LeetCode //C - 303. Range Sum Query - Immutable。
千兆以太网PHY设计:PCB原理图完整示例
深入解析千兆以太网PHY电路设计要点,提供完整的PCB原理图设计实例,帮助工程师掌握高速信号布局与电源去耦等关键技术,提升设计稳定性与信号完整性。
AD/Protel老手才知道的PCB过孔查看技巧:3分钟分清通孔、盲孔和埋孔
本文为PCB设计工程师提供了在Altium Designer/Protel中快速鉴别通孔、盲孔和埋孔的实用技巧。通过层叠管理器、高级筛选工具和实战五步法,帮助用户在3分钟内精准识别过孔类型,提升设计效率和电路性能。特别适合处理高密度HDI板卡中的复杂过孔布局。
[leetcode]303. Range Sum Query - Immutable
[leetcode]303. Range Sum Query - Immutable Analysis 周五啦,端午小长假要开始啦~—— [嘻嘻~] Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. 题目意思比较简单,就是给...
如何构建智能聊天系统
聊天分为听、思考、读,简单的通过ASR、LLM、TTS三类模型的组合可以实现,最近openai推出支持多模态的GPT-4o模型,可以把三个模型真正融合成在一起。现在市面上的模型百花齐放,各有所长。要实现可落地的方案,需要结合业务,验证模型效果,同时也要考虑方案的可行性,有些模型在国内能用,有些在国内不能用。
Leetcode 303. Range Sum Query - Immutable (Easy) (cpp)
Leetcode 303. Range Sum Query - Immutable (Easy) (cpp)
高动态范围结构光三维测量技术:从多曝光融合到深度学习的演进与应用
本文深入探讨了高动态范围结构光三维测量技术的演进与应用,从传统的多曝光融合技术到深度学习驱动的创新方案。通过工业案例展示了该技术在提升测量精度和效率方面的显著优势,特别是在处理高反光、复杂表面时的突破性表现。文章还提供了针对不同工业场景的技术选型指南,帮助工程师优化三维测量解决方案。
DB2__基本概念——联邦
建立联邦 优点:优点是数据仍然保留在原来的存储位置,而不必建立一个集中的式数据仓储。 缺点:数据联邦查询反映会慢,不适合频繁查询,而且容易出现锁争用和资源冲突等问题 建立联邦表步骤 前提条件:知道两个数据库的ip,用户名,密码及被连数据库的版本 eg: 数据库 qqdb ip: 22.5.225.225 端口:50000 密码:123456 数据库 ...
LeetCode--303. Range Sum Query - Immutable
问题链接:https://leetcode.com/problems/range-sum-query-immutable/ 这个问题属于数据结构设计的问题,属于开放式问题。 思路一:在查询时直接暴力计算,代码省略。 思路二:哈希缓存,初始化复杂度O(n^2),查询复杂度O(1),代码如下: private Map<Pair<Integer, Integer>, Inte...
CAN(2)-GD32
GD32中CAN的特征。
LeetCode303——Range Sum Query - Immutable
LeetCode303——Range Sum Query - Immutable 插播一条新题 题意: 找出数组指定两个索引之间所有元素之和。
STM32F407中文手册
STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。
Devexpress16.2.6破解工具
Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具
LeetCode: 303. Range Sum Query - Immutable
LeetCode: 303. Range Sum Query - Immutable 题目描述 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRa...
lwip-2.0.3
lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。
leetcode 303: Range Sum Query - Immutable
Range Sum Query - Immutable Total Accepted: 696 Total Submissions: 2406 Difficulty: Easy Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), i
文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况
主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。
Leetcode 303 Range Sum Query - Immutable
前缀和模版题,初始化前缀和数组并且求下标从left到right的元素和。
LeetCode 303. Range Sum Query - Immutable
LeetCode 303. Range Sum Query - Immutable (easy) 主要知识点:简单dp、构造数据结构 优先级:3(
LeetCode(303)Range Sum Query - Immutable
题目Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example:Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5)
Leetcode 303. Range Sum Query - Immutable
文章作者:Tyan 博客:noahsnail.com &nbsp;|&nbsp; CSDN &nbsp;|&nbsp; 简书 1. Description 2. Solution Version 1 class NumArray { public: NumArray(vector&lt;int&gt; nums) { this-&gt;nums = nums;
[LeetCode303] Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRan
[LeetCode] 303. Range Sum Query - Immutable
原题链接: https://leetcode.com/problems/range-sum-query-immutable/ 1. 题目介绍 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, ...
【LEETCODE】303-Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sum
【Leetcode】303. Range Sum Query - Immutable
要求设计一个数据结构,可以返回区间。
LeetCode [303. Range Sum Query - Immutable]
Problem:303. Range Sum Query - ImmutableQuestionGiven an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example:Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0
leetcode 303 Range Sum Query - Immutable C++
这个挺简单的 注意不要直接累加,会超时。 vector num; NumArray(vector &nums) { if(nums.empty()) return ; num.push_back(0); for(int i = 1 ;i<=nums.size(); i++){ num.push_back(n
1万+




被折叠的 条评论
为什么被折叠?



