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. WCF学习系列一_创建第一个WCF服务

    原创作者:灰灰虫的家http://hi.baidu.com/grayworm WCF开发实战系列一:创建第一个WCF服务 在这个实战中我们将使用DataContract,ServiceContract ...

  2. linq to ef(相当于sql中in的用法)查询语句

    select * from DoctorInfo doctor where doctor.HosDepartId in (select Id from HospitalDepartment hd wh ...

  3. 用Android-X86和VirtualBox打造高性能Android开发环境

    不知道有多少Android开发着对Android虚拟机的那悲剧的性能有意见,反正我的看法是:那速度实在是太坑爹了! 为什么Android虚拟机比iOS和WP7的虚拟机要慢很多呢?原因如下: 1. An ...

  4. gdal中文路径无法打开问题

    在C#中使用OGR读写矢量数据时,需要引用“using OSGeo.OGR;”. 同时为了处理中文路径和中文字段,需要在开始设置下面两个属性,代码如下: //为了支持中文路径,请添加下面这句代码(大多 ...

  5. Java——类比较器

    1.Product类 public class Product { private int pid; private String name; private double price; public ...

  6. asp.net WebService异步

    1 #region 异步测试 2 //委托 3 public delegate void PrintDelegate(string s); 4 [WebMethod] 5 public string ...

  7. Java实战之01Struts2-03属性封装、类型转换、数据验证

    九.封装请求正文到对象中 1.静态参数封装 在struts.xml配置文件中,给动作类注入值.调用的是setter方法. 原因:是由一个staticParams的拦截器完成注入的. 2.动态参数封装: ...

  8. Google C++编程风格指南

    作者:Hawstein 出处:http://hawstein.com/posts/google-cpp-style-guide.html 前言 越来越发现一致的编程风格的重要性,于是把Google的C ...

  9. MongoDB笔记(一)MongoDB概述和安装

    概述 关键词:关系数据库.非关系数据库 关系数据库: 关系数据库,是建立在关系数据库模型基础上的数据库,借助于集合代数等概念和方法来处理数据库中的数据.目前主流的关系数据库有oracle.SQL.ac ...

  10. HA高可用配置

    HA 即 (high available)高可用,又被叫做双机热备,用于关键性业务. 简单理解就是,有两台机器A和B,正常是A提供服务,B待命闲置,当A宕机或服务宕掉,会切换至B机器继续提供服务. 下 ...