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网络编程之Socket编程

    概念 网络编程分为BIO(传统IO).NIO.AIO.Socket编程属于BIO这种传统IO. InetAddress java.net.InetAddress是JAVA中管理IP地址的类,常用 pu ...

  2. 在使用DBMS_LOB.WRITEAPPEND()追加Clob内容的时候,报数字或值异常

    在实际的使用中有可能会使用DBMS_LOB.WRITEAPPEND()去将Clob内容作为buffer增加到我们需要的对象中,但是在实际的使用中Clob大小往往是超过32767长度的,然而DBMS_L ...

  3. Java9新特性

    转载:http://blog.csdn.net/qq_32524177/article/details/77014757 写在前面的话:Java9来了,搜索了很多关于Java9的新特性,但文献不多,特 ...

  4. PHP:第五章——字符串与数组及其他函数

    <?php header("Content-Type:text/html;charset=utf-8"); //1.str_split——将字符串转换为数组. /*$str= ...

  5. Python - learn note(1)

    1. 下载安装Python 2.7(为了向下兼容以前的版本), Python 3.5(VS2015不支持配置3.6的环境) 教程 需要使用VS2015进行开发,必须勾选上后面两项: 2. VS2015 ...

  6. bzoj1068

    题意: 给你一个未压缩串,要求你把它压缩 问你压缩后最小长度 题解: 区间dp 怎么少就怎么来 代码: #include<bits/stdc++.h> using namespace st ...

  7. react native遇到的坑

    1.模拟器报错no bundle url present https://github.com/facebook/react-native/issues/12754 http://www.cnblog ...

  8. cousera 深度学习 吴恩达 第一课 第二周 学习率对优化结果的影响

    本文代码实验地址: https://github.com/guojun007/logistic_regression_learning_rate cousera 上的作业是 编写一个 logistic ...

  9. AI产业将更凸显个人英雄主义 周志华老师的观点是如此的有深度

    今天无意间在网上看的了一则推送,<周志华:AI产业将更凸显个人英雄主义> http://tech.163.com/18/0601/13/DJ7J39US00098IEO.html 摘录一些 ...

  10. OK335xS pwm buzzer Linux driver hacking

    /**************************************************************************** * OK335xS pwm buzzer L ...