【leetcode❤python】 303. Range Sum Query - Immutable
#-*- 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的更多相关文章
- 【LeetCode】303. Range Sum Query - Immutable 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 保存累积和 日期 题目地址:https://leetcode. ...
- 【一天一道LeetCode】#303.Range Sum Query - Immutable
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
- [LeetCode] 303. Range Sum Query - Immutable (Easy)
303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...
- [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 ...
- 【leetcode❤python】 1. Two Sum
#-*- coding: UTF-8 -*- #AC源码[意外惊喜,还以为会超时]class Solution(object): def twoSum(self, nums, target): ...
- 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 ...
- Leetcode 303 Range Sum Query - Immutable
题意:查询一个数组在(i,j]范围内的元素的和. 思路非常简单,做个预处理,打个表就好 拓展:可以使用树状数组来完成该统计,算法复杂度为(logn),该数据结构强力的地方是实现简单,而且能完成实时更新 ...
- 【leetcode❤python】 112. Path Sum
#-*- coding: UTF-8 -*-# Definition for a binary tree node.# class TreeNode(object):# def __init_ ...
- 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 ...
随机推荐
- C++学习笔记 四种新式类型转换
static_cast ,dynamic_cast,const_cast,reinterpret_cast static_cast 定义:通俗的说就是静态显式转换,用于基本的数据类型转换,及指针之间的 ...
- gitlab改用ssh操作
1.配置ssh http://blog.csdn.net/xyzchenxiaolin/article/details/51853319http://blog.csdn.net/r8hzgemq/ar ...
- 通过Maven将Web程序部署到远程Tomcat8服务器的一些注意事项
1.环境变量检查(注意一定要做,否则会出现莫名其妙的错误):JAVA_HOMEM2_HOMECATALINA_HOME 2.开发环境检查,在Eclipse开发环境中最好做以下两方面的检查 2.1)Ma ...
- LUA5.3的BNF范式学习笔记
BNF巴科斯范式 {A} 表示 0 或多个 A , [A] 表示一个可选的 A chunk ::= block block ::= {stat} [retstat] stat ::= ‘;’ ...
- c/c++ qsort 函数的简单使用(1)
#include <stdio.h> #include <stdlib.h> //打印数组元素 void print(int arr[], int n){ ; i < n ...
- 【C# 进阶】事件!直接上事件!
http://www.tracefact.net/csharp-programming/delegates-and-events-in-csharp.aspx ZiYang 张,何许人也?看了他写的博 ...
- eclipse自动补全快捷键失效,sysout用不了!
好久没写Java代码了,使用新版Neon的Eclipse Java EE IDE开发时,自动补全各种失败,sysout也各种用不了, 开始还以为是电脑卡比呢,原来是版本的快捷键不同了,修改方法如下! ...
- java.sql.SQLException: ORA-00942: 表或视图不存在
1.检查JDBC数据源是否配置正确:2.检查表或视图名称是否写错:3.检查Java中数据源的数据库用户是否具有引用该表或视图的权限:
- html5,增加flash插件
<embed src="2.swf" type="" width="500" height="" >< ...
- 【原创】Android ExpandableListView使用
ExpandableView的使用可以绑定到SimpleExpandableListAdapter,主要是看这个Adapter怎么用. 这个类默认的构造函数有9个参数, 很好地解释了什么叫做又臭又长. ...