leetcode 303. Range Sum Query
来源:互联网 发布:mac tomcat端口被占用 编辑:程序博客网 时间:2024/06/06 03:25
难度:easy
Given an integer array nums, find the sum of the elements between indicesi and j (i ≤ j), inclusive.
这道题其实是一个很简单的求和,但是由于给出题目的调用方式跟以往的不一样,那就一定得用动态规划的方式。采取的是取用前面用过的值,保存了在一个临时的变量里面,然后push到一个vector里面,最后直接进行减法运算。
代码如下:
class NumArray {public: NumArray(vector<int> nums) { int temp=0; for(int i=0;i<nums.size();i++) { temp+=nums[i]; vec.push_back(temp); } } int sumRange(int i, int j) { if(i==0) return vec[j]; else return vec[j]-vec[i-1]; } private: vector<int>vec;};/** * Your NumArray object will be instantiated and called as such: * NumArray obj = new NumArray(nums); * int param_1 = obj.sumRange(i,j); */
阅读全文
0 0
- 303.[LeetCode]Range Sum Query
- LeetCode-303. Range Sum Query
- [LeetCode]303. Range Sum Query
- LeetCode#303. Range Sum Query
- leetcode 303. Range Sum Query
- [LeetCode]303. Range Sum Query
- LeetCode 303. Range Sum Query
- LeetCode 303. Range Sum Query
- LeetCode 303. Range Sum Query
- leetcode-303. Range Sum Query
- LeetCode 303. Range Sum Query
- leetcode.303.Range Sum Query
- leetcode 303. Range Sum Query
- leetcode 303. Range Sum Query
- [leetcode]: 303. Range Sum Query
- [Leetcode] 303. Range Sum Query
- [LeetCode]303. Range Sum Query
- leetcode 303. Range Sum Query
- 文本特征选择-互信息
- 数据结构之链表的实例
- log4j在spring4中的配置
- Retrofit上传头像
- Java: 对象的初始化过程
- leetcode 303. Range Sum Query
- 2 认识java虚拟机的基本结构
- StringBuffer的用法
- get请求与post提交区别的简易理解
- Caffe——Python接口学习:绘制loss和accuracy曲线
- 3 常用java虚拟机参数
- 之江学院第0届校赛决赛 A-qwb与支教(容斥+二分)
- np.where函数
- HTTP中GET与POST方法有什么区别