【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 ...
随机推荐
- leetcode-mid-array-73 set matrix zeros
mycode 空间复杂度 m+n 思路:用set把为0元素所在行.列记录下来 注意:注释的方法更快 class Solution(object): def setZeroes(self, matrix ...
- spring自动注入的三种方式
所谓spring自动注入,是指容器中的一个组件中需要用到另一个组件(例如聚合关系)时,依靠spring容器创建对象,而不是手动创建,主要有三种方式: 1. @Autowired注解——由spring提 ...
- Week13 - 376. Wiggle Subsequence
Week13 - 376. Wiggle Subsequence A sequence of numbers is called a wiggle sequence if the difference ...
- 从Android手机中取出已安装的app包,导出apk
从Android手机中取出已安装的app包,导出apk TAG:Android,提取,apk,adb,pm,root,导出apk 假设有这样一个场景,A君看到你手机上一个实用APP,想要安装到自己手机 ...
- JDK和SDK的区别:
参考链接:https://www.cnblogs.com/vaelailai/p/7976158.html jdk,是Java开发工具包,主要用于编写Java程序:也就是说你要使用Java语言,就需要 ...
- python 正则表达式 re.match
#coding:utf-8 import re #匹配内容:单词+空格+单词+任意字符 #\w 单词字符[A-Za-z0-9_] #(?P<name>...) 分组,除了原有的编号外在指定 ...
- python绘制五角星
问题描述: python中运用turtle图形模块绘制五角星 问题分析: turtle绘制图形时,得知图形中重要点的坐标非常重要. 于是,绘制五角星问题转化成为一个数学问题,计算五个顶点坐标即可. 已 ...
- jmeter分布式压测 java.io.FileNotFoundException: rmi_keystore.jks (系统找不到指定的文件。)
解决办法:1.控制机和代理机:修改jmeter.properties中server.rmi.ssl.disable=true 不启动SSL2.控制机和代理机: Slave(从压力机)启动jmeter- ...
- HDFS中NameNode发生故障没有备份从SecondNameNode恢复
1.Secondary NameNode目录结构 Secondary NameNode用来监控HDFS状态的辅助后台程序,每隔一段时间获取HDFS元数据的快照. 在/opt/module/hadoop ...
- 创建客户端项目并读取服务化的配置中心(Consul + Spring Cloud Config)
创建客户端项目并读取服务化的配置中心 将配置中心注册到服务中心(Consul) POM文件添加依赖: <dependency> <groupId>org.springframe ...