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. Java网络编程和NIO详解7:浅谈 Linux 中NIO Selector 的实现原理

    Java网络编程和NIO详解7:浅谈 Linux 中NIO Selector 的实现原理 转自:https://www.jianshu.com/p/2b71ea919d49 本系列文章首发于我的个人博 ...

  2. HDU-3853-期望/dp/坑

    LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Total Sub ...

  3. NYOJ 720 DP+二分

    项目安排 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 小明每天都在开源社区上做项目,假设每天他都有很多项目可以选,其中每个项目都有一个开始时间和截止时间,假设做完每个 ...

  4. centos6.9使用yum安装mysql(简单粗暴,亲测有效)

    第1步.yum安装mysql[root@stonex ~]#  yum -y install mysql-server安装结果:Installed:    mysql-server.x86_64 0: ...

  5. Struts2基本使用(三)--数据交互

    Struts2中的数据交互 在Struts2中我们不必再使用request.getParameter()这种方式来获取前台发送到服务器的参数. 我们可以在服务器端的Java类中直接声明一个和前台发送数 ...

  6. Java例子

    1. 本章学习总结 今天主要学习了三个知识点 封装 继承 多态 2. 书面作业 Q1. java HelloWorld命令中,HelloWorld这个参数是什么含义? 今天学了一个重要的命令javac ...

  7. Openlayers4中地图的导出

    概述: 本文讲述Openlayers4中地图的导出,包括调用天地图切片跨域.Geoserver11 WMS跨域等. 效果: 导出图片 页面展示 实现代码: document.getElementByI ...

  8. Buildroot make网卡interfaces文件被修改

    /******************************************************************************** * Buildroot make网卡 ...

  9. socket编程---UDP

    头文件 #include <sys/types.h> #include <sys/socket.h> 函数原型 int sendto (int s, const void *b ...

  10. 基于ffmpeg静态库的应用开发

    最近几天在试着做基本ffmpeg静态库的开发,只有main中包含了avdevice_register_all 或avfilter_register_all,编译就通不过,undefined refre ...