树状数组复习 leetcode 307
来源:互联网 发布:cisco 端口turnk配置 编辑:程序博客网 时间:2024/06/14 03:15
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
The update(i, val) function modifies nums by updating the element at index i to val.Example:
Given nums = [1, 3, 5]sumRange(0, 2) -> 9update(1, 2)sumRange(0, 2) -> 8
Note:
- The array is only modifiable by the update function.
- You may assume the number of calls to update and sumRange function is distributed evenly.
树状数组详解这位同学写的相当不错。。主要是有几个函数1.lowbit:树状数组用来做2分的。。很奇妙的x&-x方法。 2.add(pos,value)在pos位置加value 3.sum方法算0--pos位置的部分和。下面是树状数组的模板:
int lowbit(int pos){ return pos&(-pos); } void add(int pos, int value){ while(pos < c.size()){ c[pos] += value; pos += lowbit(pos); } } int sum(int pos){ int res = 0; while(pos > 0){ res += c[pos]; pos -= lowbit(pos); } return res; }
而leetcode的这道题,显然是就在树状数组上改了改。。需要的代码如下
void update(int i, int val) { int b = a[i]; int d= val - b; a[i] = val; add(i+1,d);}int sumRange(int i, int j) { return sum(j+1) - sum(i);//前面树状数组模板里的pos是数组下标加一的}
0 0
- 树状数组复习 leetcode 307
- 【树状数组】树状数组复习
- 树状数组复习小计
- 算法复习之------树状数组
- [复习]树状数组 敌兵布阵
- [leetcode] 树状数组 BIT
- 树状数组求区间最大值(树状数组)(复习)
- 【NOIp复习】数据结构之树状数组
- 数据结构颓废计划I-树状数组复习
- (LeetCode 307) Range Sum Query - Mutable(树状数组讲解)
- 树状数组-线段树模板题(leetcode-307)
- 数据结构与算法复习(22)—— 树状数组
- http://poj.org/problem?id=2299 树状数组 《复习》
- HDU1166 敌兵布阵 线段树||树状数组 入门题复习
- [复习]树状数组求逆序对 光荣的梦想
- (POJ 2155)Matrix (复习必看题)经典二维树状数组题目 + 树状数组的论文讲解
- leetcode.307. Range Sum Query - Mutable 树状数组
- LeetCode 315 Count of Smaller Numbers After Self(树状数组)
- Redis详细介绍
- 使用Memcached、Spring AOP构建数据库前端缓存框架
- 表单验证 对象写法
- 什么是IO(八)- 三分天下
- 【5】JAVA---地址App小软件(DeletePanel.class)(表现层)
- 树状数组复习 leetcode 307
- SqlServer部分内置函数
- cocos学习心得3
- 二维码 Demo
- spring(1)
- mysql视图查询
- 第二篇 java的编写流程
- 网络IO模型
- iOS开发:创建真机调试证书