303. Range Sum Query - Immutable(动态规划)
Example:
Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3 法一:暴力
class NumArray {
public:
vector<int> Nums;
NumArray(vector<int> nums) {
Nums =nums;
}
int sumRange(int i, int j) {
int res = ;
for(int k = i;k<=j;k++)
res+=Nums[k];
return res;
}
};
法二:
sumRange(i,j)=sum[j+1]−sum[i]
class NumArray {
public:
vector<int> Nums;
vector<int> Sums;
NumArray(vector<int> nums) {
Nums =nums;
Sums = vector<int>(nums.size()+,);
for(int i = ;i<nums.size();i++)
Sums[i+]=Sums[i]+nums[i];
}
int sumRange(int i, int j) {
int res = ;
res = Sums[j+]-Sums[i];
return res;
}
};
303. Range Sum Query - Immutable(动态规划)的更多相关文章
- 303. Range Sum Query - Immutable(动态规划)
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] 303. Range Sum Query - Immutable (Easy)
303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...
- [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 ...
- 【LeetCode】303. Range Sum Query - Immutable 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 保存累积和 日期 题目地址:https://leetcode. ...
- 【leetcode❤python】 303. Range Sum Query - Immutable
#-*- coding: UTF-8 -*- #Tags:dynamic programming,sumRange(i,j)=sum(j)-sum(i-1)class NumArray(object) ...
- Leetcode 303 Range Sum Query - Immutable
题意:查询一个数组在(i,j]范围内的元素的和. 思路非常简单,做个预处理,打个表就好 拓展:可以使用树状数组来完成该统计,算法复杂度为(logn),该数据结构强力的地方是实现简单,而且能完成实时更新 ...
- 303. Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- Java [Leetcode 303]Range Sum Query - Immutable
题目描述: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inc ...
- 【一天一道LeetCode】#303.Range Sum Query - Immutable
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
随机推荐
- ExpandableListView
ExpandableListView 1.界面 Item_Group_layout 就一个TextView <?xml version="1.0" encoding=&quo ...
- 使用CSS达到文字首尾对齐效果
在制作表单的时候经常会遇到需要不同个数的文字首尾对齐,比如: <span>姓名:</span> <span>联系方式:</span> 姓名: 联系方式: ...
- virtualbmc 使用
virtualbmc 介绍 通常情况下,我们要使用 IPMI必须使用有带外管理功能的物理机.但是在很多测试环境,我们使用的是虚拟机.virtualbmc是一个可以使用 IPMI命令来控制虚机的open ...
- 【C语言程序】输出前50个素数
#include <stdio.h> #include <stdlib.h> int main(void) { ; ; ){ ; ;i<x;i++){ ...
- [LeetCode] Kth Largest Element in a Stream 数据流中的第K大的元素
Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...
- Centos7 Nagios 搭建
Nagios 是一款自动化运维工具,可以协助运维人员监控服务器的运行状况,并且拥有报警功能.本文章将介绍其安装方法和详细的配置方法. 总结 可以做资源,网络,应用服务的监控 配置上需要配置被监控的,服 ...
- 男默女泪,ArcGIS AddIN 编辑逻辑赏析,走过路过,不要错过
看到了一段ESRI写的一个工具,我反编译了一下,学习工具中的几个代码片段 一. IEditTask task = (this._editor as IEditTaskSearch).get_TaskB ...
- python 配置文件返回的两种方式,写法不一样而已
配置文件如下: [MODE]mode:{ "register":"all"} 或者 mode = {"register":"all ...
- ASO关键词优化技巧:如何充分利用热搜榜与相关热点?
ASO关键词优化对提高市场曝光率.增加APP下载量有着至关重要的作用.那如何充分利用热搜榜与相关热点来进行ASO优化呢? 一.产品定位 因为此文主要是讲优化APP关键词的,所以产品定位这一块就 ...
- linux 常用压缩解压命令
压缩格式 打包/解压 执行命令 .tar 解包 tar ...