303. Range Sum Query - Immutable

class NumArray {
private:
vector<int> v;
public:
NumArray(vector<int> &nums) {
int n = nums.size();
v = vector<int>(n + 1);
int sum = 0;
for (int i = 1; i <= n; ++i) {
sum += nums[i - 1];
v[i] = sum;
}
} int sumRange(int i, int j) {
return v[j + 1] - v[i];
}
};
//596 ms

算法思路: sumRange(i, j) = sumRange(0, j) - sumRange(0, i - 1) (记sumRange(0, -1)=0).

时间复杂度: O(n).

空间复杂度: O(n).


C++ O(1) queries - just 2 extra lines of code

//@rantos22
class NumArray {
public:
NumArray(vector<int> &nums) : psum(nums.size()+1, 0) {
partial_sum( nums.begin(), nums.end(), psum.begin()+1);
} int sumRange(int i, int j) {
return psum[j+1] - psum[i];
}
private:
vector<int> psum;
};

使用了partial_sum函数.

[LeetCode] 303. Range Sum Query - Immutable (Easy)的更多相关文章

  1. [LeetCode] 303. Range Sum Query - Immutable 区域和检索 - 不可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  2. LeetCode 303. Range Sum Query – Immutable

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  3. Leetcode 303 Range Sum Query - Immutable

    题意:查询一个数组在(i,j]范围内的元素的和. 思路非常简单,做个预处理,打个表就好 拓展:可以使用树状数组来完成该统计,算法复杂度为(logn),该数据结构强力的地方是实现简单,而且能完成实时更新 ...

  4. Java [Leetcode 303]Range Sum Query - Immutable

    题目描述: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inc ...

  5. LeetCode 303. Range Sum Query - Immutable (C++)

    题目: Given an integer array nums, find the sum of the elements between indices iand j (i ≤ j), inclus ...

  6. LeetCode 303 Range Sum Query - Immutable(范围总和查询-永久不变)(*)

    翻译 给定一个整型数组nums,找出在索引i到j(i小于等于j)之间(包含i和j)的全部元素之和. 比如: 给定nums = [-2,0,3,-5,2,-1] sumRange(0, 2) -> ...

  7. leetcode 303. Range Sum Query - Immutable(前缀和)

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  8. 【LeetCode】303. Range Sum Query - Immutable 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 保存累积和 日期 题目地址:https://leetcode. ...

  9. 【一天一道LeetCode】#303.Range Sum Query - Immutable

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

随机推荐

  1. IE下onchange事件不立即执行

    做前端开发免不了为浏览器的兼容而劳神,所以坚持把发现的浏览器兼容问题做做总结,是很有意义的. 比如IE8及以下的浏览器的onchange事件实在该控件失去焦点之后才执行的,也就是要点一下空白的地方,才 ...

  2. Webstorm 不识别es6 import React from ‘react’——webstorm不支持jsx语法怎么办

    2016-10-31更新 webstorm不支持es6语法怎么办? webstorm不支持jsx语法怎么办? 参考:webstorm不支持jsx语法怎么办 I spent ages trying to ...

  3. eclipse 好用的快捷键

    Ctrl+Shift+X 把当前选中的文本全部变味小写 Ctrl+Shift+Y 把当前选中的文本全部变为小写 Alt+Shift+C 修改函数结构(比较实用,有N个函数调用了这个方法,修改一次搞定) ...

  4. FOR XML PATH 转换问题

    以下我带大家了解关于 FOR XML PATH 首先我们看下所熟悉的表数据 之后转换 <骨牌编号>1</骨牌编号> <骨牌颜色>橙</骨牌颜色> < ...

  5. 在.Net中进行跨线程的控件操作(下篇:BackgroundWorker)

    在.Net中,如果我们在非UI线程上访问窗体上的控件的时候,会产生一个跨线程调用的异常,那么如何处理这种情况呢?在上一章中,我介绍了使用Control.Invoke方法,如果你不习惯使用委托,那么.N ...

  6. 搭建showslow:前端性能跑分及优化工具

    综述:showslow是一个开源的工具,集成并通过Yahoo yslow.google page speed.dynaTrace AJAX等工具监测网站各项性能指标,然后通过图表和排名展示出来. 1. ...

  7. Asp.net Mvc HTTP 404。

    asp.net mvc  设置完起始页的时候会出现以下Error 此错误的原因是在MVC中设置完起始页 会改变   的 值 从而使服务器找不到 相对应的路径   解决方案: 通过 路由设置,解决web ...

  8. Scala - error: not found: value SortedMap

    先 IMPORT!!!! scala> import scala.collection._import scala.collection._ scala>  SortedMap(" ...

  9. python 自动化之路 day 08_2 网络编程

    本节内容 Socket介绍 Socket参数介绍 基本Socket实例 Socket实现多连接处理 通过Socket实现简单SSH 通过Socket实现文件传送 作业:开发一个支持多用户在线的FTP程 ...

  10. python相关博客

    入门:http://www.pythontip.com/ Python之禅--大道至简 美胜于丑,显胜于隐,简胜于繁,繁胜于杂,平胜于迭,疏胜于密,读胜于写...名可名, 请常名 http://www ...