[LeetCode] Range Sum Query - Immutable
The idea is fairly straightforward: create an array accu
that stores the accumulated sum fornums
such that accu[i] = nums[0] + ... + nums[i]
in the initializer of NumArray
. Then just return accu[j + 1] - accu[i]
in sumRange
. You may try the example in the problem statement to convince yourself of this idea.
The code is as follows.
C++
class NumArray {
public:
NumArray(vector<int> &nums) {
accu.push_back();
for (int num : nums)
accu.push_back(accu.back() + num);
} int sumRange(int i, int j) {
return accu[j + ] - accu[i];
}
private:
vector<int> accu;
}; // Your NumArray object will be instantiated and called as such:
// NumArray numArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);
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] # Your NumArray object will be instantiated and called as such:
# numArray = NumArray(nums)
# numArray.sumRange(0, 1)
# numArray.sumRange(1, 2)
[LeetCode] Range Sum Query - Immutable的更多相关文章
- [LeetCode] Range Sum Query - Immutable & Range Sum Query 2D - Immutable
Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indice ...
- [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——Range Sum Query - Immutable
Question Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...
- [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query 2D - Immutable
Very similar to Range Sum Query - Immutable, but we now need to compute a 2d accunulated-sum. In fac ...
- [LeetCode] 303. Range Sum Query - Immutable (Easy)
303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...
- [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] 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
303. Range Sum Query - Immutable Easy Given an integer array nums, find the sum of the elements betw ...
随机推荐
- ASP.NET MVC 下拉列表使用小结
ASP.NET MVC中下拉列表的用法很简单,也很方便,具体来说,主要是页面上支持两种Html帮助类的方法:DropDownList()和DropDownListFor().这篇博文主要作为个人的一个 ...
- Sql Server 调用DLL
背景 在处理数据或者分析数据时,我们常常需要加入一定的逻辑,该些处理逻辑有些sql是可以支持,有些逻辑SQL则无能为力,在这种情况下,大多数人都会编写相关的程序来处理成自己想要的数据,但每次处理相同逻 ...
- jQuery自动分页打印表格(HTMLtable),可以强制换页
最近做项目的时候需要做批量打印订单,一个订单可能在最后一页是的内容是不足一页的,这时候下一个订单需要下一页打印,这样就需要强制换页.在下一页再打印下一个订单 部分代码: 部分重要的css是分页的换页的 ...
- JavaBean与Jsp
这一节我们总结一下JavaBean和Jsp的关系. 1. JavaBean javaBean是一个遵循特定写法的Java类,它通常具有如下特点: 1)这个java类必须具有一个无参构造函 ...
- Django网站管理、后台、模型(Admin)
1. 启动wampserver 2. 新建一个名为mysite的项目django-admin.py startproject mysite 3. 新建一个叫做blog的appcd mysitepyth ...
- asp.net mvc通过预处理实现数据过滤和数据篡改。
需求特别简单.在 Controller加过滤器.实现在所有的方法上增加id=12312321312.另外将price篡改为price+5. 这样做可以最大的减少代码的改动量,人员变动厉害,业务也模糊了 ...
- /etc/pam.d 与 /etc/security 密码策略
PAM(Pluggable Authentication Modules )是由Sun提出的一种认证机制.它通过提供一些动态链接库和一套统一的API,将系统提供的服务 和该服务的认证方式分开,使得系统 ...
- LPC43xx I2S
- solr详解,开发必备
1.基础知识 创建索引的过程如下: (1).建立索引器IndexWriter,这相当于一本书的框架 (2).建立文档对象Document,这相当于一篇文章 (3).建立信息字段对象Field,这相当于 ...
- select选择框内容左右移动添加删除栏(升级)
先看一下之前的版本(10年前的作品了) 新版增加了拖动事件(双向及本列),双击左右自动移动,修正了算法性能更好: 也更新了如果姓名长度太长显示变形问题