【leetcode】307. Range Sum Query - Mutable
题目如下:

解题思路:就三个字-线段树。这个题目是线段树用法最经典的场景。
代码如下:
class NumArray(object):
def __init__(self, nums):
"""
:type nums: List[int]
"""
self.nl = nums
self.tree = []
if len(nums) == 0:
return
for i in xrange((4*len(nums)+1)):
self.tree.append([])
self.build(1,len(nums),1,nums)
#print self.tree
def build(self,l,r,k,nums):
self.tree[k] = [l,r]
if l == r: #leaf
self.tree[k].append(nums[l-1])
return nums[l-1]
wl = self.build(l,(l+r)/2,2*k,nums)
wr = self.build((l+r)/2+1,r,2*k+1,nums)
#print l,r,wl,wr
self.tree[k].append(wl+wr)
return self.tree[k][2]
def update(self, i, val):
"""
:type i: int
:type val: int
:rtype: void
"""
if i > len(self.nl):
return
diff = self.nl[i] - val
#for j in range(i,len(self.sl)):
# self.sl[j] -= diff
self.nl[i] = val
k = 1
i += 1
while True:
self.tree[k][2] -= diff
m = (self.tree[k][0] + self.tree[k][1])/2
if self.tree[k][0] == self.tree[k][1]:
break
if i <= m:
k = 2*k
else:
k = 2*k + 1
#print self.tree
def calcRange(self,i,j,k):
#print i,j,k
if i == self.tree[k][0] and j == self.tree[k][1]:
return self.tree[k][2]
m = (self.tree[k][0] + self.tree[k][1])/2
if j <= m:
return self.calcRange(i,j,2*k)
elif i > m:
return self.calcRange(i,j,2*k+1)
else:
return self.calcRange(i,m,2*k) + self.calcRange(m+1,j,2*k+1)
def sumRange(self, i, j):
"""
:type i: int
:type j: int
:rtype: int
"""
#print self.sl
#print self.nl
return self.calcRange(i+1,j+1,1)
# Your NumArray object will be instantiated and called as such:
# obj = NumArray(nums)
# obj.update(i,val)
# param_2 = obj.sumRange(i,j)
【leetcode】307. Range Sum Query - Mutable的更多相关文章
- 【刷题-LeetCode】307. Range Sum Query - Mutable
Range Sum Query - Mutable Given an integer array nums, find the sum of the elements between indices ...
- 【LeetCode】304. Range Sum Query 2D - Immutable 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 预先求和 相似题目 参考资料 日期 题目地址:htt ...
- 【LeetCode】303. Range Sum Query - Immutable 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 保存累积和 日期 题目地址:https://leetcode. ...
- [LeetCode] 307. Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- 【刷题-LeetCode】304. Range Sum Query 2D - Immutable
Range Sum Query 2D - Immutable Given a 2D matrix matrix, find the sum of the elements inside the rec ...
- leetcode笔记:Range Sum Query - Mutable
一. 题目描写叙述 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...
- 307. Range Sum Query - Mutable
题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...
- leetcode@ [307] Range Sum Query - Mutable / 线段树模板
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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
随机推荐
- Python爬虫数据保存到MongoDB中
MongoDB是一款由C++语言编写的非关系型数据库,是一个基于分布式文件存储的开源数据库系统,其内容存储方式类似于JSON对象,它的字段值可以是其它文档或数组,但其数据类型只能是String文本型. ...
- 关于 token
用户在浏览器做一系列操作,后台服务怎么判断这些操作是来自同一个用户? 1. seesion 用户登录后,后台生成 sessionid 返回给浏览器,浏览器的每次请求带上 sessionid,后台关联 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第3节 接口_1_接口概述与生活举例
- 测开之路一百:jquery引用、语法、事件
工作中一般会使用jquery代替js,jquery官网:https://jquery.com/ 引用jquery: 第一种方式:下载引用: jquery下载官网:https://jquery.com/ ...
- Python 使用openpyxl导出Excel表格的时候,使用save()保存到指定路径
在使用openpyxl导出Excel表格的使用,如何指定导出的路径呢. 使用sava(filename),会保存到当前执行文件的路径下. 使用sava("/tmp/{}.xlsx" ...
- js数组,运算符
- linux安装JSONCPP
#tar -zxf scons-2.1.0.tar.gz #cd scons-2.1.0 #python setup.py install #tar -zxf jsoncpp-src-0.5.0. ...
- 20191105 《Spring5高级编程》笔记-第9章
第9章 事务管理 一些名词: 2PC(2 Phase Commit) XA协议 JTS(Java Transaction Service) JCA(Java EE Connector Architec ...
- SpringBoot使用webservice
Pom.xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spr ...
- python学习第五十天shutil模块的用法
什么shutil模块,就是对高级的文件,文件夹,压缩包进行处理的模块,下面简单讲述其用法. 文件和文件夹的操作 拷贝文件内容 import shutil shutil.copyfileobj(open ...