Range Sum Query - Immutable
2015-12-20 12:04
435 查看
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.
Subscribe to see which companies asked this question
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.
Subscribe to see which companies asked this question
class NumArray { private: vector<int> sumUntilNow; int curSum; public: NumArray(vector<int> &nums) { curSum = 0; for (int i=0; i<nums.size(); i++) { curSum += nums[i]; sumUntilNow.push_back(curSum); } } int sumRange(int i, int j) { if (i == 0) return sumUntilNow[j]; return sumUntilNow[j]-sumUntilNow[i-1]; } }; // Your NumArray object will be instantiated and called as such: // NumArray numArray(nums); // numArray.sumRange(0, 1); // numArray.sumRange(1, 2);
相关文章推荐
- poj 2533 Longest Ordered Subsequence 最长递增子序列(LIS)
- [iOS]iOS8可用的识别用户方式(idfa、UUID、idfv)
- [Android] 使用Fragment创建动态UI
- rp++ find ROP sequences
- EasyUI之DataGrid使用
- 303. Range Sum Query - Immutable
- 织梦dedecms整合Ueditor1.4.3百度编辑器(最新版2015年12月整合)
- UIScrollView源码分析(一)
- iOS UI进阶-6.0 手势
- 初识Power Builder9.0
- MVC jsonModelBuilder
- UE编辑技巧
- hdu5568 sequence2 dp+大数
- 如何在Autolayout的模式下动态设置 UITableView的 tableHeader 的高度
- UVAlive2531 The K-League(最大流)
- UI交互设计的网站
- 动态修改UINavigationBar的背景色
- Leetcode: Implement Queue using Stacks
- IPtables中SNAT和MASQUERADE的区别
- QuickContactBadge 自定义获取联系人