LeetCode OJ:Range Sum Query - Immutable(区域和)
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解决,注意0这个点的特殊情况就可以了 ,代码如下:
class NumArray {
public:
NumArray(vector<int> &nums) {
sum.resize(nums.size());
for(int i = ; i < nums.size(); ++i){
sum[i] = i == ? nums[] : nums[i] + sum[i - ];
}
}
int sumRange(int i, int j) {
if(i == )
return sum[j];
return sum[j] - sum[i - ];
}
private:
vector<int> sum;
};
LeetCode OJ: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 ...
- [LeetCode] 307. Range Sum Query - Mutable 区域和检索 - 可变
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] 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
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
题意:查询一个数组在(i,j]范围内的元素的和. 思路非常简单,做个预处理,打个表就好 拓展:可以使用树状数组来完成该统计,算法复杂度为(logn),该数据结构强力的地方是实现简单,而且能完成实时更新 ...
- 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(58)-Range Sum Query - Immutable
题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...
- LeetCode 303. Range Sum Query - Immutable (C++)
题目: Given an integer array nums, find the sum of the elements between indices iand j (i ≤ j), inclus ...
- LeetCode 303 Range Sum Query - Immutable(范围总和查询-永久不变)(*)
翻译 给定一个整型数组nums,找出在索引i到j(i小于等于j)之间(包含i和j)的全部元素之和. 比如: 给定nums = [-2,0,3,-5,2,-1] sumRange(0, 2) -> ...
随机推荐
- tensorflow训练自己的数据集实现CNN图像分类2(保存模型&测试单张图片)
神经网络训练的时候,我们需要将模型保存下来,方便后面继续训练或者用训练好的模型进行测试.因此,我们需要创建一个saver保存模型. def run_training(): data_dir = 'C: ...
- maven 介绍(二)
本文内容主要摘自:http://www.konghao.org/index 内部视频 三.仓库 仓库:本地仓库:远程仓库:私有仓库(nexus) 1. nexus 的安装: 1). 下载并且解压缩 2 ...
- sublime 安装插件报错
sublime 安装插件报错,大部分原因是本地防火墙开启了,关闭本地防火墙
- 漂亮的输出-----prettytable和colorama的使用
Python通过prettytable模块将输出内容如表格方式整齐输出,python本身并不内置,需要独立安装该第三方库. 1 pip install PrettyTable 1 #源码安装 2 wg ...
- myeclipse 方法上加上@Override就报错的处理方法
在有@Override方法上面会报错如下: The method oncreate(Bundle) of type HelloWorld must override or implement a su ...
- FFmpeg 入门(2):输出视频到屏幕
本文转自:FFmpeg 入门(2):输出视频到屏幕 | www.samirchen.com SDL 我们这里使用 SDL 来渲染视频到屏幕.SDL 是 Simple Direct Layer 的缩写, ...
- Cloudera Manager安装之时间服务器和时间客户端(二)
福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 Java全栈大联盟 ...
- python 数据分析----matplotlib
Matplotlib是一个强大的Python绘图和数据可视化的工具包. 安装方法:pip install matplotlib 引用方法:import matplotlib.pyplot as plt ...
- Jquery编历数组
<html> <head> <title>编历</title> <script type="text/javascript"& ...
- win10 安装 tensorflow 并运行helloworld
win10 安装 tensorflow 并运行helloworld 折腾了一下,在win10上成功安装tensorflow. 1 下载安装python,注意一定要是64位(比如python-3.5 ...