题目如下:

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

代码如下:

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. leetcode-mid-Linked list- 105. Construct Binary Tree from Preorder and Inorder Traversal

    mycode   43.86% # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, ...

  2. duliu题之狼抓兔子题解

    拖了将近5天的正解和AC.........emmmmm........... 事实告诉我们这种毒瘤题一定要建双向边(用了不知道多少个小时质疑建边的人欲哭无泪) 心态爆炸的传送 题了个面 这是个求最小割 ...

  3. sh/bash/csh/Tcsh/ksh/pdksh等shell的区别

    w shell confusion..what is diff between bash, ksh, csh, tcsh..??  http://www.linuxquestions.org/ques ...

  4. 图书-软件架构:《Design Patterns: Elements of Reusable Object-Oriented Software》(即后述《设计模式》一书)

    ylbtech-图书-软件架构:<Design Patterns: Elements of Reusable Object-Oriented Software>(即后述<设计模式&g ...

  5. selenium启动firefox打开导入向导问题解决

    操作系统:win8-64位 火狐版本:40.0.2 问题描述:selenium启动firefox时,每次启动都提示我导入其他浏览器的页签,如下图所示 解决方法一: 到firefox的profiles. ...

  6. TypeError: list indices must be integers or slices, not str

    错误如下: TypeError: list indices must be integers or slices, not str 错误代码块: aa是一组list套dict数据 函数insert接收 ...

  7. Sqlserver 中间表的操作

    去除列名重复 select rtrim(ltrim(a.Bank)) as Country, count(*) as Total from T_BASE_OWNER a left join T_BAS ...

  8. C# datatable 导出到Excel

    datatable导出到Excel /// <summary> /// 将DataTable导出为Excel文件(.xls) /// </summary> /// <pa ...

  9. Noi2018 归途

    zz:https://blog.csdn.net/dreaming__ldx/article/details/81106748 以海拔为第一关键字对边进行从大到小的排序,然后修建kruskal重构树, ...

  10. 16 (H5*) JS第6天 日期、字符串、数组

    目录 1:date 2:格式化日期 3:字符串 4:字符串的方法 5:字符串的案例 6:数组的方法 7:包装类型 复习 正文 1:日期:date <script> //创建实例对象 // ...