【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 ...
随机推荐
- 在裸机centos7系统中部署django项目的过程
概要 本文用一台安装了centos7.5系统的裸奔Linux机器(当然是虚拟机)详细讲解从无到有部署django项目的过程. 安装必要的工具 配置yum源 至于什么是yum源大家请自行百度,本人用的是 ...
- linux文本图形界面转换
vim /etc/inittab 3为默认进入文本界面, 5为默认进入图形界面 文本界面下输入init5或者startx切换图形化界面 图形化界面下输入init3切换文本界面
- 1 基础架构:一条sql查询语句如何执行?
1 基础架构:一条sql查询语句如何执行? 分析一个最简单的查询 mysql> select * from T where ID=10: MySQL基本架构示意图 大体来说,mysql可以分为s ...
- VUe.js 父组件向子组件中传值及方法
父组件向子组件中传值 1. Vue实例可以看做是大的组件,那么在其内部定义的私有组件与这个实例之间就出现了父子组件的对应关系. 2. 父子组件在默认的情况下,子组件是无妨访问到父组件中的数据的,所以 ...
- webpack前端模块打包器
webpack前端模块打包器 学习网址: https://doc.webpack-china.org/concepts/ http://www.runoob.com/w3cnote/webpack-t ...
- C# DropDownList绑定添加新数据的三种方法
一.在前台手动绑定 <asp:DropDownList ID="DropDownList1" runat="server"> <asp: ...
- [2019杭电多校第十场][hdu6701]Make Rounddog Happy
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6701 题目大意为求满足 $max(a_{l},a_{l+1}\cdot \cdot \cdot a_{ ...
- 10、应用机器学习的建议(Advice for Applying Machine Learning)
10.1 决定下一步做什么 到目前为止,我们已经介绍了许多不同的学习算法,如果你一直跟着这些视频的进度学习,你会发现自己已经不知不觉地成为一个了解许多先进机器学习技术的专家了. 然而,在懂机器学习的人 ...
- window.onload后跟函数 和跟函数名的区别【window.onload = asd() 和 window.onload = asd 】
window.onload:页面加载完毕执行[DOM tree + 外部图片 + 资源] <script> function asd(){ return 10; } window.onlo ...
- TimeUnit类 java.util.concurrent.TimeUnit
TimeUnit是什么? TimeUnit是java.util.concurrent包下面的一个类,表示给定单元粒度的时间段 主要作用 时间颗粒度转换 延时 常用的颗粒度 TimeUnit.DAYS ...