leetcode, Range Sum Query - Mutable
2016-01-03 12:42
351 查看
class NumArray(object): def __init__(self, nums): """ initialize your data structure here. :type nums: List[int] """ self.nums = [0]*len(nums) self.kmp = [0] * ( len(nums)+1 ) for ind in range(len(nums)): self.update( ind, nums[ind] ) def lowbit(self, x): return x & (-x) def update(self, i, val): """ :type i: int :type val: int :rtype: int """ Å diff = val - self.nums[i] self.nums[i] += diff pos = i+1 while pos<len(self.kmp): self.kmp[pos] += diff pos = pos + self.lowbit(pos) def sumRange(self, i, j): """ sum of elements nums[i..j], inclusive. :type i: int :type j: int :rtype: int """ def summ(n): temp, pos = 0, n while pos>0: temp += self.kmp[pos] pos = pos - self.lowbit(pos) return temp if i==0: return summ(j+1) return summ(j+1) - summ(i) # Your NumArray object will be instantiated and called as such: # numArray = NumArray(nums) # numArray.sumRange(0, 1) # numArray.update(1, 10) # numArray.sumRange(1, 2)
相关文章推荐
- burpsuite的基础应用-暴力破解
- soap request by afnetworking2.X/3.X
- 自定义UISearchBar和UISearchDisplayController
- poj 2533_Longest Ordered Subsequence
- Hadoop-2.2.0中文文档—— Common - Native Libraries Guide
- Gradle for Android 第二篇( Build.gradle入门 )
- 比较两个图片UIImage是否相同
- UITableViewCell点击样式
- Invalidate以及Invalidate(false)和Invalidate(true)的区别
- ActionBar 不能显示logo 的问题 ,背景颜色的修改, 以及ActionBar 的官方guide
- 【About Queue】(待改)
- 【答读者问】如何入门和提高嵌入式
- 从锤子手机罗永浩到T型人才
- HDU-3530 Subsequence(好题)
- UIImage-拉伸问题
- UIButton-内边距
- UIButton-内部布局
- HDU 2062 Subset sequence
- UIButton
- 将UITableView封装到极致