LeetCode——Range Sum Query - Immutable
Question
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.
Solution
简单的动态规划,依次累加,table[j] - table[i - 1]就表示两者之间的求和,注意i - 1可能越界。
Code
class NumArray {
public:
NumArray(vector<int> nums) {
int total = 0;
for (int i = 0; i < nums.size(); i++) {
total += nums[i];
table.push_back(total);
}
}
int sumRange(int i, int j) {
return (table[j] - ((i - 1) < 0 ? 0 : table[i - 1]));
}
private:
vector<int> table;
};
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(i,j);
*/
LeetCode——Range Sum Query - Immutable的更多相关文章
- [LeetCode] Range Sum Query - Immutable & Range Sum Query 2D - Immutable
Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indice ...
- [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] Range Sum Query - Immutable
The idea is fairly straightforward: create an array accu that stores the accumulated sum fornums suc ...
- [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query 2D - Immutable
Very similar to Range Sum Query - Immutable, but we now need to compute a 2d accunulated-sum. In fac ...
- [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 2D - Mutable 二维区域和检索 - 可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] 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
303. Range Sum Query - Immutable Easy Given an integer array nums, find the sum of the elements betw ...
随机推荐
- [iOS微博项目 - 3.6] - 获取未读消息
github: https://github.com/hellovoidworld/HVWWeibo A.获取登陆用户未读消息 1.需求 获取所有未读消息,包括新微博.私信.@.转发.关注等 把未 ...
- Linux系统CPU核数等信息查看
版权声明:本文为原创文章,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明. https://blog.csdn.net/fgf00/article/details/52584 ...
- python基础知识体系
一.编程风格.语法要求.变量格式.基本数据类型.运算.流程控制.用户交互 二.字符串.列表.元组.字典.迭代器和生成器 三.函数.内置函数.文件操作.异常处理.模块.常用模块.lambda.yield ...
- linux下的时间管理概述
2017/6/21 时间这一概念在生活中至关重要,而在操作系统中也同样重要,其在系统中的功能绝不仅仅是给用户提供时间这么简单,内核的许多机制都依赖于时间子系统.但凡是要在某个精确的时间执行某个事件,必 ...
- javascrpt 页面格式化页面
下面这个页面,格式化javaScript <html> <head> <title>JS格式化工具 </title> <meta http-equ ...
- celery-rabbitmq 安装部署
一:Python安装 1.下载python3源码 wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz 2.解压 tar xf P ...
- docker部署oracle
oracle部署 创建oracle用户 [root@stpass-15 ~]# useradd oracle[root@stpass-15 oracle]# cd oracle [root@stpas ...
- 前端 html border-right: 1px solid red;
后边框 加粗 实体线 红色 border-right: 1px solid red;
- UVA10100:Longest Match(最长公共子序列)&&HDU1458Common Subsequence ( LCS)
题目链接:http://blog.csdn.net/u014361775/article/details/42873875 题目解析: 给定两行字符串序列,输出它们之间最大公共子单词的个数 对于给的两 ...
- sdut3140 A*B(math)
题目:传送门 题目描述 Your task is to find the minimal positive integer number Q so that the product of digits ...