public class NumArray
{
List<int> list = new List<int>();
public NumArray(int[] nums)
{
var sum = ;
for (int i = ; i < nums.Length; i++)
{
sum += nums[i];
list.Add(sum);
}
} public int SumRange(int i, int j)
{
if (i == )
{
return list[j];
}
else
{
return list[j] - list[i - ];
}
}
} /**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.SumRange(i,j);
*/

https://leetcode.com/problems/range-sum-query-immutable/#/description

补充一个python的实现:

 from itertools import accumulate
class NumArray: def __init__(self, nums: List[int]):
self.dp = list(accumulate(nums)) def sumRange(self, i: int, j: int) -> int:
if i == :
return self.dp[j]
else:
return self.dp[j] - self.dp[i-]

leetcode303的更多相关文章

  1. [Swift]LeetCode303. 区域和检索 - 数组不可变 | Range Sum Query - Immutable

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

  2. leetcode303 Range Sum Query - Immutable

    """ Given an integer array nums, find the sum of the elements between indices i and j ...

随机推荐

  1. For循环重复代码的重构

    DRY(don't repeat yourself),重复往往是代码腐烂的开始,我们一般的处理手法是将重复的代码提取成一个方法,然后用新方法替换掉原来的代码. 但是对于for循环里面的重复代码要如何处 ...

  2. 『转』三星推出Android智能手表Galaxy Gear

    苹果定下来本月10日召开新品发布会,而它的竞争对手三星却抢先一步.今天凌晨,三星在德国柏林一口气发布了三款重量级产品.三星智能手表Galaxy Gear最引人关注,其将于9月25日陆续在全球上市,售价 ...

  3. icon fonts入门

    iconfont网站 http://www.iconfont.cn(推荐) http://fontello.com/ http://fontawesome.io/   https://icomoon. ...

  4. New Concept English Two 28 76

    $课文74  舞台之外 784. An ancient bus stopped by a dry river bed and a party of famous actors and actresse ...

  5. Java SE 8 流库(一)

    1. 流的作用 通过使用流,说明想要完成什么任务,而不是说明如何去实现它,将操作的调度留给具体实现去解决: 实例:假如我们想要计算某个属性的平均值,那么我们就可以指定数据源和属性,然后,流库就可以对计 ...

  6. 一行能装逼的JavaScript代码的延伸

    前段就是坑,入坑水真深. 先看看一个黑科技, 纳尼,这是什么东西. (!(~+[])+{})[--[~+""][+[]]*[~+[]] + ~~!+[]]+({}+[])[[~!+ ...

  7. Web验证方式(1)--Http Basic Authentication

    Http Basic Authentication是HTTP协议中定义的Web系统中的验证方式.参考wiki 主要的实现机制如下: 1. 用户通过浏览器匿名访问web资源. 2. web服务器检测到w ...

  8. linux 线程的同步 三 (内存信号量的使用)

    信号量.同步这些名词在进程间通信时就已经说过,在这里它们的意思是相同的,只不过是同步的对象不同而已.但是下面介绍的信号量的接口是用于线程的信号量,注意不要跟用于进程间通信的信号量混淆,关于用于进程间通 ...

  9. bzoj4331: JSOI2012 越狱老虎桥

    Description 这里,是美丽的南京:这里,是秀美的进香河:这里是安逸的老虎桥.  如果说进香河的美,美在其秀美的风光,倒不如说是美在了那惬意的南京古典小巷式生活.如果说进香河的迷人,在其淳朴的 ...

  10. C++ 实例化对象 p->printX()

    一.从栈实例化对象 我们首先定义一个类,类的名字叫TV,里面包括两个成员变量,两个成员函数. class TV // 定义一个电视的类TV { public: ]; // 定义类的属性,一个数组 in ...