[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.
Example:
Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3
Note:
- You may assume that the array does not change.
- 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 区域和检索 - 不可变的更多相关文章
- 303 Range Sum Query - Immutable 区域和检索 - 不可变
给定一个数组,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点.例如:给定nums = [-2, 0, 3, -5, 2, -1],求和函数为sumRange() ...
- [LeetCode] 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 (Easy)
303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...
- [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 ...
- 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) -> ...
随机推荐
- Codeforces G. Ciel the Commander
题目描述: Ciel the Commander time limit per test 1 second memory limit per test 256 megabytes input stan ...
- SpringBoot启动流程及其原理
Spring Boot.Spring MVC 和 Spring 有什么区别? 分别描述各自的特征: Spring 框架就像一个家族,有众多衍生产品例如 boot.security.jpa等等:但他们的 ...
- drf框架总结复习(1)
Serializers 序列化组件 为什么要用序列化组件 当我们做前后端分离的项目~~我们前后端交互一般都选择JSON数据格式,JSON是一个轻量级的数据交互格式. 那么我们给前端数据的时候都要转成j ...
- Centos7-安装py3
安装依赖 yum install gcc openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel li ...
- 项目alpha冲刺-测试
作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Alpha冲刺 团队名称 基于云的胜利冲锋队 项目名称 云评:高校学生成绩综合评估及可视化分析平台 这个作业的 ...
- 关于微信小程序在ios中无法调起摄像头问题
这几天关于微信小程序开发关于wx.chooseVideo组件问题,因为自己一直是安卓手机上测试,可以调取摄像头,但是应用在ios上无法打开摄像头,困扰了好多天,经过反复查看官方文档,今天总算修复了这个 ...
- JVM JDK1.8 以后的新特性 VisualVM的安装使用
一.JVM在新版本的改进更新以及相关知识 1.JVM在新版本的改进更新 图中可以看到运行时常量池是放在方法区的 1.1对比: JDK 1.6 及以往的 JDK 版本中,Java 类信息.常量池.静态变 ...
- Django 中使用redis
Django使用redis 方式一,使用Django-redis模块 #安装: pip3 install django-redis CACHES = { "default": ...
- LightOJ - 1326 - Race(DP)
链接: https://vjudge.net/problem/LightOJ-1326 题意: Disky and Sooma, two of the biggest mega minds of Ba ...
- js改变this指向
js中修改this的指向 方法整理 call,apply,bind 以上的三哥方法都是用来改变js中this的指向 call 使用方法:fun.call(thisArg[,arg1[, arg2[, ...