leetcode-303. Range Sum Query - Immutable
2016-04-27 21:50
471 查看
Given an integer array nums, find the sum of the elements >between indices i and j (i ≤ j), inclusive.
Example:
思路:他给了两个函数:第一个遍历时顺带把array[i]变为前i个的和,这样计算时做个减法就行了
Example:
思路:他给了两个函数:第一个遍历时顺带把array[i]变为前i个的和,这样计算时做个减法就行了
class NumArray { //思路,在NumArray中求出每个索引对应的前n个的和,这样最后求值只只要做个减法就行了 vector<int> myNum; public: NumArray(vector<int> &nums) { for(int i=1;i<nums.size();i++) { nums[i] = nums[i-1]+nums[i]; } myNum = nums; } int sumRange(int i, int j) { //保证j>=i if(i == 0) { return myNum[j]; } return myNum[j] - myNum[i-1]; } }; // Your NumArray object will be instantiated and called as such: // NumArray numArray(nums); // numArray.sumRange(0, 1); // numArray.sumRange(1, 2);
相关文章推荐
- Android 图文混排spannableStringBuilder简单使用
- IDEA 主题、字体、编辑区主题、文件编码修改
- 设置UITabBarController的用法
- js/jquer获取web页面上传文件的大小
- sdut 2605 Mountain Subsequences(树状数组)
- Implement Stack using Queues
- xcode7.3出现the behavior of the UICollectionViewFlowLayout is not defined警告
- Codeforces--237E--Build String(最小费用流)
- note:debugging requires the debug connect session system privilege
- RxJS入门(4)----深入Sequence
- sql gui 工具
- 自定义UITableViewCell左滑显示编辑按钮
- phalcon builder get raw sql
- iOS UI调试神器,插件injection for Xcode使用方法
- 完美限制UITextField的输入长度
- Codeforces Beta Round #11 A. Increasing Sequence 贪心
- 线程池.(Executors,ThreadPoolExecutor,BlockingQueue,RejectedExecutionHandler).介绍
- android 消息队列 ConcurrentLinkedQueue
- iOS 自定义UIPickerView天数选择器视图 —— HERO博客
- Android bluetooth介绍(四): a2dp connect流程分析