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) -> ...
随机推荐
- eclipse中添加tomcat ServerName 无法输入
Eclipse的开发过程中,无法从以下方式,添加Tomcat服务器. 其中ServerName是被置为灰色的,无法编辑. 如何解决 1. 关闭Eclipse 2. 打开WorkSpace所在的 ...
- java内存回收
java中引用类型 强引用 Persnon p = new Person(); 当指向Person对象的引用计数为0时,Person对象才能被垃圾回收器回收. 软引用 SoftReference&l ...
- Django框架搭建(windows系统)
Django框架搭建(windows系统) 一.Django简介 开放源代码的Web应用框架,由Python语言编写,一个大而全的框架. 1.web框架介绍 具体介绍Django之前,必须先介绍WEB ...
- WCF可靠性会话之服务分流
可靠性绘画答条件: (1)服务绑定支持可靠性绘画:特别是netTcpBinding.wsHttpBinding及wsDualHttpBinding. wsDualHtttpBinding可靠性绘画始终 ...
- click事件多次触发 jQuery
jQuery 中 click事件会累计绑定 例如下列代码: aNode.click(function(){ bNode.click(function(){ console.log('haha'); } ...
- mybatis家族
mybatis 优秀的持久层框架,它支持定制化SQL.存储过程以及高级映射. 备注:通过mapper实现数据库与实体类相互映射 MyBatis 避免了几乎所有的JDBC 代码和手动设置参数以及获取结果 ...
- flex 的总结
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- .NET中常见的锁 笔记
Lock.Monitor lock和Monitor的区别 1.lock的底层本身是Monitor来实现的,所以Monitor可以实现lock的所有功能. 2.Monitor有TryEnter的功能,可 ...
- C语言一些总结
预处理#include<stdio.h>:头文件. stdio.h 标准输入输出. string.h 字符串预处理,. int main()主函数,返回的是int整型, return 0; ...
- OpenDayLight "Error executing command: java.lang.NullPointerException"问题解决
参考: Fedora 21 mostly working but NullPointerException at Karaf shell 在使用ODL的时候,安装功能组件时出现: Error exec ...