[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 ...
随机推荐
- HDU-1002(简单大数加法)
A + B Problem II Problem Description I have a very simple problem for you. Given two integers A and ...
- com.sun.org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException
在日志中, 查看导入的包是否是 import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;
- WPF-TxtBox控件利用KeyDown来控制键盘输入
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e) { TextBox txt = ...
- WPF动画之路径动画(3)
XAML代码: <Window x:Class="路径动画.MainWindow" xmlns="http://schemas.microsoft.com/winf ...
- Moving Tables
Moving Tables Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- Hive - 建表和加载数据指令小结 以及使用Load data指令的注意事项
类似Mysql的数据库概念: hive> CREATE DATABASE cui; hive> USE cui; 创建表: CREATE TABLE test( first STRING, ...
- python相关博客
入门:http://www.pythontip.com/ Python之禅--大道至简 美胜于丑,显胜于隐,简胜于繁,繁胜于杂,平胜于迭,疏胜于密,读胜于写...名可名, 请常名 http://www ...
- C程序的构成及动态内存分配
对一个程序,通常的理解就是,源码编译成机器代码,然后通过机器解释运行.不过是怎样编译成机器代码,和怎样运行的,无疑是个值得探讨的问题.怎样编译成机器代码,过程就是源码的编译.链接,编译器做了这些事.而 ...
- ASP.NET MVC5 easyui 之 treegrid 初用记录
菜鸟初次使用,参考论坛中介绍的方法仍走了一些弯路,把自己遇到的问题记录下来. 1.必须定义根节点: 2.根节点一个或多个均可: 4.根节点的父节点属性不必定义,或者定义为0: 5.各级子节点的父节点属 ...
- js 判断数组中是否存在
/* 判断数组中是否存在 var somearray = ["mon", "tue", "wed", "thur"] s ...