【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 ...
随机推荐
- Java语言程序设计(基础篇) 第七章 一维数组
第七章 一维数组 7.2 数组的基础知识 1.一旦数组被创建,它的大小是固定的.使用一个数组引用变量,通过下标来访问数组中的元素. 2.数组是用来存储数据的集合,但是,通常我们会发现把数组看作一个存储 ...
- JFinal学习
1 jfinal-1.9-bin.jar 2 继承Controller编写控制器 public void sendJPushToXXX() { String userId = getPara(&quo ...
- redhat note
1,iptables -I INPUT 5 -m state --state NEW -p tcp --dport 80 -j ACCEPT
- mongodb群集
项目目标:故障自动切换和自动修复成员节点,各个数据库之间数据完全一致.项目描述:副本集没有固定主节点,是整个集群选举得出的一个主节点,当其不工作时变 更其他节点.最小的副本集也应该具备一个pri ...
- c语言的学习秘籍之链表
刚翻出来的作品,有点低级,但希望能起到作用: #include<stdio.h>#include<stdlib.h>#include<time.h>#include ...
- .NET中表单的JS验证
JS验证代码如下:(需要引入两个JS包) <script type="text/javascript" src="/js/jquery.validate.min.j ...
- 常用git命令总结
这些命令是最常用的,一般的提交代码.拉取代码.合并代码.分支切换等等操作用这些命令就足够了. 1.git init 把一个目录初始化成git仓库 2.git add test.txt 把文 ...
- String 与StringBuilder有什么区别
C# String 对象是不可改变的.每次使用 System.String 类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间.在需要对字符串执行重复修改的情况下,与 ...
- unresolved inclusion in the java header in JNI
eclipse的ndk开发环境建差不多后打开jni的samples里的hello-jni项目.添加native和运行都没有问题,但是打开hello-jni.c看到一片红: 光这一个文件牵涉的问题有下面 ...
- android 布局学习
各种layout用到的一些重要属性 第一类:属性值为true或false android:layout_centerHrizontal 水平居中 android:layout_centerVertic ...