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

Example:

Given nums = [-2, 0, 3, -5, 2, -1]

sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3

Note:

  1. You may assume that the array does not change.
  2. There are many calls to sumRange function.

给一个整数数组,找出两个index之间的数字和。

如果每次遍历i, j之间的数字相加求和,十分的不高效,无法通过OJ。

解法:DP,建一个数组dp,其中dp[i]表示[0, i]区间的数字之和,那么[i,j]就可以表示为dp[j]-dp[i-1],当i=0时,直接返回dp[j]即可。

Java:

class NumArray {
int[] nums; public NumArray(int[] nums) {
for(int i = 1; i < nums.length; i++)
nums[i] += nums[i - 1]; this.nums = nums;
} public int sumRange(int i, int j) {
if(i == 0)
return nums[j]; return nums[j] - nums[i - 1];
}
}  

Python:

class NumArray(object):
def __init__(self, nums):
"""
initialize your data structure here.
:type nums: List[int]
"""
self.accu = [0]
for num in nums:
self.accu += self.accu[-1] + num, def sumRange(self, i, j):
"""
sum of elements nums[i..j], inclusive.
:type i: int
:type j: int
:rtype: int
"""
return self.accu[j + 1] - self.accu[i] 

Python:

# Time:  ctor:   O(n),
# lookup: O(1)
# Space: O(n)
class NumArray(object):
def __init__(self, nums):
"""
initialize your data structure here.
:type nums: List[int]
"""
self.accu = [0]
for num in nums:
self.accu.append(self.accu[-1] + num), def sumRange(self, i, j):
"""
sum of elements nums[i..j], inclusive.
:type i: int
:type j: int
:rtype: int
"""
return self.accu[j + 1] - self.accu[i] 

Python: wo

class NumArray(object):

    def __init__(self, nums):
"""
:type nums: List[int]
"""
s, n = 0, len(nums)
self.dp = [0] * (n + 1)
for i in xrange(n):
s += nums[i]
self.dp[i + 1] = s def sumRange(self, i, j):
"""
:type i: int
:type j: int
:rtype: int
"""
return self.dp[j + 1] - self.dp[i]  

C++:

class NumArray {
public:
NumArray(vector<int> &nums) {
accu.push_back(0);
for (int num : nums)
accu.push_back(accu.back() + num);
} int sumRange(int i, int j) {
return accu[j + 1] - accu[i];
}
private:
vector<int> accu;
};  

C++:

class NumArray {
public:
NumArray(vector<int> &nums) {
dp.resize(nums.size() + 1, 0);
for (int i = 1; i <= nums.size(); ++i) {
dp[i] = dp[i - 1] + nums[i - 1];
}
}
int sumRange(int i, int j) {
return dp[j + 1] - dp[i];
} private:
vector<int> dp;
};

C++:

class NumArray {
public:
NumArray(vector<int> &nums) {
dp = nums;
for (int i = 1; i < nums.size(); ++i) {
dp[i] += dp[i - 1];
}
}
int sumRange(int i, int j) {
return i == 0? dp[j] : dp[j] - dp[i - 1];
}
private:
vector<int> dp;
};

  

类似题目:

[LeetCode] 304. Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

Range Sum Query - Mutable

Range Sum Query 2D - Mutable

  

 

All LeetCode Questions List 题目汇总

[LeetCode] 303. Range Sum Query - Immutable 区域和检索 - 不可变的更多相关文章

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

    给定一个数组,求出数组从索引 i 到 j  (i ≤ j) 范围内元素的总和,包含 i,  j 两点.例如:给定nums = [-2, 0, 3, -5, 2, -1],求和函数为sumRange() ...

  2. [LeetCode] 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 (Easy)

    303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...

  4. [LeetCode] 307. Range Sum Query - Mutable 区域和检索 - 可变

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

  5. 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 ...

  6. Leetcode 303 Range Sum Query - Immutable

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

  7. 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 ...

  8. 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 ...

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

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

随机推荐

  1. 动态生成16位不重复随机数、随机创建2位ID

    /** 1. * 动态生成16位不重复随机数 * * @return */ public synchronized static String generate16() { StringBuffer ...

  2. C#各版本

    C#各版本 本系列文章主要整理并介绍 C# 各版本的新增功能. C# 8.0 C#8.0 于 2019年4月 随 .NET Framework 4.8 与 Visual Studio 2019 一同发 ...

  3. JQuery系列(3) - 工具方法

    jQuery函数库提供了一个jQuery对象(简写为$),这个对象本身是一个构造函数,可以用来生成jQuery对象的实例.有了实例以后,就可以调用许多针对实例的方法,它们定义jQuery.protot ...

  4. nginx和ftp搭建图片服务器

    一.需要的组件 图片服务器两个服务: Nginx(图片访问): 1.http服务:可以使用nginx做静态资源服务器.也可以使用apache.推荐使用nginx,效率更高. 2.反向代理 实现 负载均 ...

  5. Net-NTLMv1的利用思路

    Net-NTLMv1的加密方法: 客户端向服务器发送一个请求 服务器接收到请求后,生成一个16位的Challenge,发送回客户端 客户端接收到Challenge后,使用登录用户的密码hash对Cha ...

  6. omnibus-gitlab 架构学习

    omnibus-gitlab是gitlab 团队fork 自chef 的omnibus 项目,同时做了一些自定义的开发,omnibus-gitlab 简化了 gitlab 的部署以及维护,同时里边集成 ...

  7. 交互设计算法基础(4) - Hash Table

    import java.util.Map; // Note the HashMap's "key" is a String and "value" is an ...

  8. 【luoguP5091】【模板】欧拉定理

    题目链接 欧拉定理: 当\(a\),\(m\)互质时,\(a^{\phi(m)}\equiv 1 (mod ~ m)\) 扩展欧拉定理: 当\(B>\phi(m)\)时,\(a^B\equiv ...

  9. 转载:理解scala中的Symbol

    相信很多人和我一样,在刚接触Scala时,会觉得Symbol类型很奇怪,既然Scala中字符串都是不可变的,那么Symbol类型到底有什么作用呢? 简单来说,相比较于String类型,Symbol类型 ...

  10. js获取url中的参数,并保证获取到的参数不乱码

    //网上比较经典的js获取url中的参数的方法 function getQueryString(name) { var reg = new RegExp("(^|&)" + ...