[LeetCode] 303. Range Sum Query - Immutable (Easy)
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)的更多相关文章
- [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 ...
- 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 ...
- Leetcode 303 Range Sum Query - Immutable
题意:查询一个数组在(i,j]范围内的元素的和. 思路非常简单,做个预处理,打个表就好 拓展:可以使用树状数组来完成该统计,算法复杂度为(logn),该数据结构强力的地方是实现简单,而且能完成实时更新 ...
- 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 ...
- 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 ...
- LeetCode 303 Range Sum Query - Immutable(范围总和查询-永久不变)(*)
翻译 给定一个整型数组nums,找出在索引i到j(i小于等于j)之间(包含i和j)的全部元素之和. 比如: 给定nums = [-2,0,3,-5,2,-1] sumRange(0, 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 ...
- 【LeetCode】303. Range Sum Query - Immutable 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 保存累积和 日期 题目地址:https://leetcode. ...
- 【一天一道LeetCode】#303.Range Sum Query - Immutable
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
随机推荐
- C#垃圾回收机制详解
一.托管代码/非托管代码 C#代码通过C#编译器编译成程序集,程序集由微软中间语言组成,CLR会为程序集开辟一个应用程序域,程序集就是运行在这个应用程序域里面的,应用程序域是相互独立的,互不影响. 托 ...
- string应用
今天在网上搜了一些资料. C# string类应用 判断是否包含子串 想要判断一个字符串中是否包含某个子串,可以用Contains方法来实现: ? public bool Contains (stri ...
- 转:四种方案解决ScrollView嵌套ListView问题
转载自:http://blog.sina.com.cn/s/blog_46798aa80101lxbk.html 原始的连接已经不知道是哪里了,项目中遇到了同样的问题,花了一下午都没有想到是嵌套引起的 ...
- Windows 7中,用Visual Studio开发WPF应用程序,实现从Windows Explorer中拖拽文件到应用程序,始终显示“无法拖放”符号问题解决方案
Are you running your application or Visual Studio that hosts the app under administrative privilege? ...
- mysql 直接从date 文件夹备份表,还原数据库之后提示 table doesn`t exist的原因和解决方法
补充:正常情况下,建议数据库备份最好用工具进行备份,通过拷贝数据库表进行数据迁移,不同的环境会出现各种不同的意外问题. 背景:今天在整理一个网站的时候,操作系统由于系统自动更新导致一直出现系统蓝屏死机 ...
- 响应者链 hittest:withEvent: 方法的使用
关于响应者链部分的基础内容 参考http://www.cnblogs.com/wendingding/p/3795171.html 这里我要说明的是 关于- (UIView *)hitTest:(CG ...
- oc文件基本读写及操作
代码: #import <Foundation/Foundation.h> //NSString 写文件 void stringWriteToFile(){ NSString *path ...
- 将表单数据转化为json数据
/** * 将Form表单转成符合后台要求的json格式数据 * @param frm form表单Id * * @return json格式的数据 */function getFormJson(fr ...
- 引用传递&值传递
下面的程序阐述了值传递与应用传递的区别. package com.liaojianya.chapter1; /** * This program demonstrates the use of arr ...
- mini2440移植uboot-2008.10 (二) DM9000网卡驱动移植
还是利用 mini2440移植uboot-2008.10 (一) 修改好的代码 通过观察可以发现,mini2400使用的网卡芯片是DM9000,在uboot-2008.10源码中已经支持该芯片的驱动 ...