#-*- 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. web前端开发培训和自学 哪种选择更适合你

    web前端相对于其他软件开发是比较容易入门的,但是如果深入学习就比较困难了,这门技能需要从业人员掌握一定的设计.代码.交互技能和一些SEO技能,容易入门还涉及这么多知识和技能,那学习web前端开发到底 ...

  2. SpringMVC常用配置-处理程序异常以及404错误

  3. Redis不同类型方法整合

    1     对value操作的命令 exists(key):确认一个key是否存在 del(key):删除一个key type(key):返回值的类型 keys(pattern):返回满足给定patt ...

  4. Windows Phone 二、WP控件

  5. C#调用windows API的一些方法

    使用C#调用windows API(从其它地方总结来的,以备查询) C#调用windows API也可以叫做C#如何直接调用非托管代码,通常有2种方法: 1.  直接调用从 DLL 导出的函数. 2. ...

  6. sql语句修改字段长度

    sql语句修改字段长度 alter table <表名> alter column <字段名> 新类型名(长度) 例: alter table students alter c ...

  7. (五)SQL入门 数据库查询

    什么是查询?查询就是Select语句对数据库的探究. 查询是一种目的,一种需求,一种期望.是Select语句去实现的.Select语句不是只是指select语句,而是多个子句一起使用得组合. sele ...

  8. android下面使用SurfaceView+ mediaPlayer播放视频

    final SurfaceView surfaceView = new SurfaceView(StartupActivity.this); StartupActivity.this.mediaPla ...

  9. struts2漏洞与修复

    步骤: 1.下载struts-2.3.16.3-all, D:\TEST\struts2.3.16.3 2.替换jar,参考 http://blog.csdn.net/spyjava/article/ ...

  10. 学习笔记:MySQL数据库初步 概念

    数据库 什么是数据库,数据库就是对大量信息进行管理的高效的解决方案,按照数据结构来组织.存储和管理数据的载体.而我们常说的数据库可以分为关系型和非关系型数据库. 关系型数据库 这里说的关系型数据库,是 ...