LeetCode学习篇十一——Range Sum Query - Immutable
来源:互联网 发布:java语言程序设计下载 编辑:程序博客网 时间:2024/05/16 16:01
题目: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
难度:easy 通过率:26.0%
这道题借用一个数组记录从第0项加到第i项的和t[i],然后在return的时候直接减掉多余的项即可。算法复杂度:O(n)
class NumArray {public: vector<int> t; NumArray(vector<int> &nums) { t = vector<int>(nums.size(), 0); for(int i = 0; i < nums.size(); i++) { if(i > 0) t[i] = t[i-1]+nums[i]; else t[0] = nums[0]; } } int sumRange(int i, int j) { if(i == 0) return t[j]; return t[j]- t[i-1]; }};
0 0
- LeetCode学习篇十一——Range Sum Query - Immutable
- [LeetCode] Range Sum Query - Immutable
- [LeetCode]Range Sum Query - Immutable
- LeetCode Range Sum Query - Immutable
- LeetCode Range Sum Query - Immutable
- LeetCode:Range Sum Query - Immutable
- [Leetcode]Range Sum Query - Immutable
- leetcode Range Sum Query - Immutable
- leetcode- Range Sum Query - Immutable
- leetcode:Range Sum Query - Immutable
- Leetcode: Range Sum Query - Immutable
- Leetcode: Range Sum Query - Immutable
- 【leetcode】:Range Sum Query - Immutable
- leetcode--Range Sum Query - Immutable
- LeetCode Range Sum Query - Immutable
- LeetCode|Range Sum Query - Immutable
- LeetCode:Range Sum Query - Immutable
- 【Leetcode】Range Sum Query - Immutable
- 深度学习(DL)与卷积神经网络(CNN)学习随笔-05-基于Python的LeNet之CNN
- 118. Pascal's Triangle (python)
- 通过Shiro完成基础的身份认证
- 关于Android ANR的思考
- 使用TextView实现ImageView效果
- LeetCode学习篇十一——Range Sum Query - Immutable
- MySQL——数据库语言+语法
- HPC —— 高性能计算
- 119. Pascal's Triangle II (python)
- node.js利用javascript中构造函数继承,实现模块调用
- android快速实现毛玻璃效果
- 121. Best Time to Buy and Sell Stock (python)
- 书本ISBN号码校验
- 省市联动(struts2+ajax+非数据库)