【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 ...
随机推荐
- Windowns 10打开此电脑缓慢问题的一种解决办法
上个月刚配的台式,i7 6700K + 16GB + PM961用起来爽得不行. 不过最近两天突然发现,打开"此电脑"总会卡住,窗口里面也不显示磁盘.地址栏缓慢刷新. 一般此类问题 ...
- kdiff3的主窗口说明 Base Local Remote 分别代表什么分支
- 树状sql--采用递归方式获取节点
创建数据库 create table City(id varchar(3) primary key , pid varchar(3) , name varchar(10)) 插入数据 insert i ...
- jQuery 跨域访问的三种方式 No 'Access-Control-Allow-Origin' header is present on the reque
问题: XMLHttpRequest cannot load http://v.xxx.com. No 'Access-Control-Allow-Origin' header is present ...
- RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.0 版新增查询引擎管理
欲了解V3.0版本的相关内容可查看下面的链接地址. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.0 版本发布 RDIFramework.NET — 基于.NET的快速信 ...
- 通过Chrome浏览器检测和优化页面
1.访问(http://www.cnblogs.com/viaiu/) 2.点击F12 前两步就在扯淡 3.点击Audits标签,进入测试界面 4.点击按钮开始检测 5.如下图可以进行页面加载资源的详 ...
- listener does not currently know of SID项目部署报数据库错
百度以后是数据库配置错误啊,但是我觉得就是对的呀,也去验证过了. 反正知道问题就是在databaseurl那里,但是原因是什么呢?其他地方部署都是好的呀! 集群问题啊,数据库人员采用集群方式配置数据库 ...
- mac 安装jdk1.5
前期准备 Java安装包 JDK 1.5:Java for Mac OS X 10.5 Update 10(From: Apple) 辅助工具 Pacifist:用于提取*.dmg安装包中的文件(点我 ...
- [原创]java WEB学习笔记108:Spring学习---基于配置文件的形式实现AOP
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Keras学习~第一个例子~跑MNIST
import numpy as npimport gzip import struct import keras as ks import logging from keras.layers impo ...