#-*- coding: UTF-8 -*-
#Tags:dynamic programming,sumRange(i,j)=sum(j)-sum(i-1)
class NumArray(object):
    sums=[]
    def __init__(self, nums):
        """
        initialize your data structure here.
        :type nums: List[int]
        """
        self.nums=nums
        self.sums=nums
        for i in xrange(1,len(self.sums)):
            self.sums[i]+=self.sums[i-1]
        

    def sumRange(self, i, j):
        """
        sum of elements nums[i..j], inclusive.
        :type i: int
        :type j: int
        :rtype: int
        """
        if i>j or j>=len(self.sums):return 0
       
        return self.sums[j] if i==0 else (self.sums[j]-self.sums[i-1])

numArray = NumArray([-2, 0, 3, -5, 2, -1])
print numArray.sumRange(3,4)

【leetcode❤python】 303. Range Sum Query - Immutable的更多相关文章

  1. 【LeetCode】303. Range Sum Query - Immutable 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 保存累积和 日期 题目地址:https://leetcode. ...

  2. 【一天一道LeetCode】#303.Range Sum Query - Immutable

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

  3. [LeetCode] 303. Range Sum Query - Immutable (Easy)

    303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...

  4. [LeetCode] 303. Range Sum Query - Immutable 区域和检索 - 不可变

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

  5. 【leetcode❤python】 1. Two Sum

    #-*- coding: UTF-8 -*- #AC源码[意外惊喜,还以为会超时]class Solution(object):    def twoSum(self, nums, target):  ...

  6. LeetCode 303. Range Sum Query – Immutable

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

  7. Leetcode 303 Range Sum Query - Immutable

    题意:查询一个数组在(i,j]范围内的元素的和. 思路非常简单,做个预处理,打个表就好 拓展:可以使用树状数组来完成该统计,算法复杂度为(logn),该数据结构强力的地方是实现简单,而且能完成实时更新 ...

  8. 【leetcode❤python】 112. Path Sum

    #-*- coding: UTF-8 -*-# Definition for a binary tree node.# class TreeNode(object):#     def __init_ ...

  9. Java [Leetcode 303]Range Sum Query - Immutable

    题目描述: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inc ...

随机推荐

  1. CentOS 7 安装字体库 & 中文字体

    前言 报表中发现有中文乱码和中文字体不整齐(重叠)的情况,首先考虑的就是操作系统是否有中文字体,在CentOS 7中发现输入命令查看字体列表是提示命令无效:  如上图可以看出,不仅没有中文字体,连字体 ...

  2. jenkins配置邮件

    1.安装 Email Extension Plugin 插件 2.进入系统管理--系统设置 3.按照如下图设置 首先找到 Extended E-mail Notification  

  3. lua自定义迭代器

    迭代器 http://www.tutorialspoint.com/lua/lua_iterators.htm 迭代器能够让你遍历某个集合或者容器中的每一个元素. 对于lua来说, 集合通常指代 ta ...

  4. sql存储过程异常捕获并输出例子还有不输出过程里面判断异常 例子

    编程的异常处理很重要,当然Sql语句中存储过程的异常处理也很重要,明确的异常提示能够快速的找到问题的根源,节省很多时间. 下面,我就以一个插入数据为例来说明Sql Server中的存储过程怎么捕获异常 ...

  5. Oracle中较长number型数值的科学计数显示问题

    表中有id列,类型为number(38).在sqlplus中查询的时候,查询结果的显示方式为科学计数法: ID ---------- 4.5572E+18 4.5574E+18 4.5585E+18 ...

  6. android 多媒体数据库(非原创)

    推荐文章:http://fzlihui.iteye.com/blog/1097952,http://www.cnblogs.com/pen-ink/archive/2011/06/02/2068410 ...

  7. java List<Item> its=new ArrayList<Item>(); Map按value中的某字段排序

    public List<Item> getAllItem(){ Map<Long, Item> itemDic = new HashMap<Long, Item>( ...

  8. jquery cdn/////////////////zzz

    jquery-2.1.1 注:jquery-2.0以上版本不再支持IE 6/7/8)百度引用地址 (推荐目前最稳定的,不会出现延时打不开情况) 百度压缩版引用地址:<script src=&qu ...

  9. The constructor BASE64Encoder() is not accessible due to restriction on required library

    在Eclipse中编写Java代码时,用到了BASE64Decoder,import sun.misc.BASE64Decoder;可是Eclipse提示:Access restriction : T ...

  10. 为什么C#中要设计IntPtr?

    示例代码: IntPtr vertex = someObj.Get().Lock(0, someObj.Get().GetSizeInBytes(), HardwareBuffer.LOCKOPTIO ...