LeetCode 303. Range Sum Query - Immutable
2016-04-24 09:15
363 查看
Given an integer array nums, find the sum of the elements between indices
i and j (i ≤ j), inclusive.
Example:
Note:
You may assume that the array does not change.
There are many calls to sumRange function.
Since the elements are not immutable, we can just use simple DP to solve it.
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.
Since the elements are not immutable, we can just use simple DP to solve it.
class NumArray { private: vector<int> dp; public: NumArray(vector<int> &nums) { dp.resize(nums.size() + 1, 0); for(int i = 1; i <= nums.size(); ++i) { dp[i] = dp[i-1] + nums[i-1]; } } int sumRange(int i, int j) { return dp[j + 1] - dp[i]; } };
相关文章推荐
- 搜狗输入法人机交互设计的用户体验
- LeetCode 225. Implement Stack using Queues
- hdu 1005 Number Sequence
- UIAlertController使用技巧
- easyui datagrid-filter数据网格过滤的汉化
- Android UI优化之ViewStub标签的使用
- Android UI优化之merge标签的使用
- Maximum Subsequence Sum[PAT1007][PTA01-复杂度2]——动态规划
- intercepting IO request
- mathtype需要安装一个较新版本的MT Extra (TrueType)字体
- Android UI优化之include标签的使用
- String,StringBuffer与StringBuilder的区别
- ZOJ 3940 Modulo Query
- android studio之build.gradle写法简介
- UEFI和Le 4000 gacy启动U盘安装系统的区别
- Java Specification Requests
- java--StringBuilder
- 【arduino】常见的几个传感器的使用
- request.getParameter() 和request.getAttribute() 区别
- Exported service does not require permission问题。