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. 使用canvas制作在线画板

    canvas绘图的强大功能,让人前仆后继的去研究它.代码全部加起来不足百行.还用到了h5中的<input type="color"/>和<input type=& ...

  2. ie各个版本hack

    /*类内部hack:*/ .header {_width:100px;} /* IE6专用*/ .header {*+width:100px;} /* IE7专用*/ .header {*width: ...

  3. Web电子商务网(三层)V2.0源码

    Web电子商务网(三层)V2.0源码 源码描述: 一.源码特点     采用三层架构开发,购物车功能 二.功能介绍 前台集成了产品在线展示,用户注册.在线调查.在线投稿 后台有类别管理\图书管理\订单 ...

  4. 20151211Jquery Ajax进阶学习笔记

    四.JSON 和 JSONP 如果在同一个域下,$.ajax()方法只要设置 dataType 属性即可加载 JSON 文件.而在非 同域下,可以使用 JSONP,但也是有条件的. //$.ajax( ...

  5. 将Excel,ppt和word转化为html

    有些时候可能需要将Excel,ppt和word转化为html在页面上显示.我从网上查到一些代码,记录在这里以供需要的朋友参考 1.将word转化为html显示 //================== ...

  6. 归并排序算法(C#实现)

    归并排序(Merge Sort)是利用"归并"技术来进行排序.归并是指将若干个已排序的子文件合并成一个有序的文件.归并排序有两种方式:1): 自底向上的方法 2):自顶向下的方法 ...

  7. HTML标签_Form

    理解HTML是如何跳转到java程序中去的:Form常用HTML标签的作用<body> <form action="servlet/UploadServlet" ...

  8. ios NSHashTable & NSMapTable

    在ios开发中大家用到更多的集合类可能是像NSSet或者NSDictionary,NSArray这样的.这里要介绍的是更少人使用的两个类,一个是NSMapTable,另一个是NSHashTable. ...

  9. Oracle hextoraw和rawtohex

    Oracle hextoraw和rawtohex [日期:2012-07-17] 来源:Linux社区  作者:adrain_001 [字体:大 中 小]     HEXTORAW  语法: HEXT ...

  10. 常用JS正则表达式收集

    1.去掉字符串前后空格,不会修改原有字符串,返回新串.str.replace(/(^\s*)|(\s*$)/g,'');