题目如下:

解题思路:就三个字-线段树。这个题目是线段树用法最经典的场景。

代码如下:

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的更多相关文章

  1. 【刷题-LeetCode】307. Range Sum Query - Mutable

    Range Sum Query - Mutable Given an integer array nums, find the sum of the elements between indices ...

  2. 【LeetCode】304. Range Sum Query 2D - Immutable 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 预先求和 相似题目 参考资料 日期 题目地址:htt ...

  3. 【LeetCode】303. Range Sum Query - Immutable 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 保存累积和 日期 题目地址:https://leetcode. ...

  4. [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 ...

  5. 【刷题-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 ...

  6. leetcode笔记:Range Sum Query - Mutable

    一. 题目描写叙述 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...

  7. 307. Range Sum Query - Mutable

    题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...

  8. 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 ...

  9. 【一天一道LeetCode】#303.Range Sum Query - Immutable

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

随机推荐

  1. Linux内核调试方法总结之序言

    本系列主要介绍Linux内核死机.异常重启类稳定性问题的调试方法. 在Linux系统中,一切皆为文件,而系统运行的载体,是一类特殊的文件,即进程.因此,我尝试从进程的角度分析Linux内核的死机.异常 ...

  2. __main__ — Top-level script environment

    w 29.4. __main__ — Top-level script environment — Python 3.6.1 documentation  https://docs.python.or ...

  3. Ffmpeg AAC 编码错误 Input contains (near) NaN/+-Inf

    Ffmpeg AAC编码 如果传入参Frame的Sample Format 为 AV_SAMPLE_FMT_S16,会出现 错误提示 Input contains (near) NaN/+-Inf,需 ...

  4. ThreadPoolExecutor 源码分析

    ThreadPoolExecutor 线程池核心实现类 线程池的生命周期 RUNNING: 接受新任务,同时处理工作队列中的任务 SHUTDOWN: 不接受新任务,但是能处理工作队列中的任务 STOP ...

  5. 认识DOM(上)

    认识DOM 文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 先来看看下面代码 ...

  6. 类Random

    /* * Random:产生随机数的类 * * 构造方法 * public Random();没有给种子,用的是默认种子,是当前时间的毫秒值 * public Random(long seed);使用 ...

  7. tensorflow和pytorch的区别

    pytorch是动态框架,tensorflow是静态框架 针对tensorflow,我们先构造了一个计算图,构建完之后,这个计算图就不能改变了,我们再开启会话,输入数据,进行计算.那么这个流程就是固定 ...

  8. 安全运维 - Windows系统应急响应

    挖矿病毒应急 传播方式: 通过社工.钓鱼方式下载和运行了挖矿程序(邮件.IM等) 利用计算机系统远程代码执行漏洞下载.上传和执行挖矿程序. 利用i算计Web或第三方软件漏洞获取计算机权限,然后下载和执 ...

  9. 编译的时候出现"/usr/bin/ld: cannot find -lz

    编译的时候出现"/usr/bin/ld: cannot find -lz"错误,需要安装zlib-dev这个包,在线安装命令为:apt-get install zlib1g-dev ...

  10. PTA第四周作业

    一.本周完成的作业 7-2 选择法排序 (20 分) 本题要求将给定的n个整数从大到小排序后输出. 输入格式: 输入第一行给出一个不超过10的正整数n.第二行给出n个整数,其间以空格分隔. 输出格式: ...