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. 卸载oracle11g步骤图解

    卸载oracle11g步骤图解       重启电脑即可

  2. PHP:第五章——字符串的概念

    <?php header("Content-Type:text/html;charset=utf-8"); //字符串概念: //1.单引号.//里面的变量不会被解释 //例 ...

  3. Python自动化运维之路-01

    python的主要应用 python的擅长领域 学python有没有前途?python的语言排名 语言选择 运维会了开发后可以干什么? python的最大优势就是什么都能做. 课程概述 毕业目标 周五 ...

  4. Mac iStat Menu 注册码

    9185-4915-3252-3716-0000 1574-5977-7956-8062-0000 6015-5448-3282-4975-0000 9665-5955-6856-2071-0000 ...

  5. Office 365 开发 集成VS2013 (一)

    博客地址 http://blog.csdn.net/foxdave 题外话:好久不写了,个人比较懒,有时候想写东西的时候想一想就又不知从何下笔了.之前因为某些机缘发现自己完全是个管理外行,所以最近下了 ...

  6. npm 与 package.json 快速入门

    npm 是前端开发广泛使用的包管理工具,之前使用 Weex 时看了阮一峰前辈的文章了解了一些,这次结合官方文章总结一下,加深下理解吧! 读完本文你将了解: 什么是 npm 安装 npm 更新 npm ...

  7. 接口测试工具Soapui5.1.2参数化之Properties20150924

    上次用天气预报的来给大家演示了下如何创建项目.测试套件.测试用例的操作,今天演示下如何参数化,废话不多说,跟着操作即可: 1.在一个用例中有两个步骤,我们想将第一个步骤中的响应中的值,传入第二个步骤中 ...

  8. ubuntu16.04安装Nvidia显卡驱动、CUDA8.0和cudNN V6

    Nvidia显卡驱动安装 在ubuntu搜索框输入 软件更新,打开 "软件和更新" 对话框,在 附加驱动里选择系统检测到的Nvidia驱动,应用更改,重启系统: 安装完成之后查看G ...

  9. Linux内核编译技巧

    1.将多个文件编译成一个模块,部分文件可选 Example1: drivers/usb/core/Makefile:usbcore-y := usb.o hub.o hcd.o urb.o messa ...

  10. pthread线程初始化(pthread_once)

    pthread_once 语法 int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)); #include ...