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. Leetcode 18

    class Solution { public: vector<vector<int>> fourSum(vector<int>& nums, int ta ...

  2. C#对象初始或器-Chapter3 P38

    protected string GetMessage() { //如何构造和初始化泛型集合库中的一个数组和两个类. Product myProduct = ,Name="Kayak&quo ...

  3. 【hive】关于用户留存率的计算

    首先用户留存率一般是面向新增用户的概念,是指某一天注册后的几天还是否活跃,是以每天为单位进行计算的.一般收到的需求都是一个时间段内的新增用户的几天留存 (1)找到这个时间段内的新增用户(也可能含有地区 ...

  4. 安装MySQL后要做的事

    安装MySQL后要修改的配置 [mysql] default-character-set=utf8 [mysqld] # 关闭域名反解 skip_name_resolve # 每表一个独立的表空间文件 ...

  5. 硬盘安装CentOS 6.0(超级详细图文教程)

    硬盘安装CentOS 6.0(超级详细图文教程) 来源:   引言: 电脑系统是Windows XP,电脑没有光驱.手头没有U盘.没有移动硬盘.电脑主板不支持U盘启动,在这种情况下想安装CentOS ...

  6. 保卫萝卜官方PC版——含绿色版 V1.0.6Beta

    官方网站 | 安装版 | 绿色版

  7. NodeJS反序列化漏洞利用

    原文来自:http://www.4hou.com/web/13024.html node.js是一个服务器端的运行环境,封装了Google V8引擎,V8引擎执行JavaScript速度非常快,性能非 ...

  8. web 安全相关(一):Log注入(转)

    在网上有很多关于安全的文章,但是 OWASP 开发指导 涵 盖了几乎所有关于Web站点安全的东西.(注:OWASP(开放Web应用安全项目- Open Web Application Security ...

  9. flask第二十二篇——模板【4】过滤器

    请关注微信公众号:自动化测试实战 先来教大家一个pycharm设置默认模板的方法.我们每次新建模板或者平时写代码打开以后可能都要重复写# coding: utf-8这些代码,其实我们可以设置好模板,让 ...

  10. docker 数据卷 ---- 进阶篇

    笔者在<Docker 基础 : 数据管理>一文中介绍了 docker 数据卷(volume) 的基本用法.随着使用的深入,笔者对 docker 数据卷的理解与认识也在不断的增强.本文将在前 ...