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:

  1. Given nums = [1, 3, 5]
  2.  
  3. sumRange(0, 2) -> 9
  4. update(1, 2)
  5. sumRange(0, 2) -> 8

Note:

  1. The array is only modifiable by the update function.
  2. You may assume the number of calls to update and sumRange function is distributed evenly.

利用线段树解决动态区间求值问题、

  1. class NumArray {
  2.  
  3. class STreeNode {
  4. int sum;
  5. int start;
  6. int end;
  7. STreeNode left, right;
  8. STreeNode(int start, int end) {
  9. this.start = start;
  10. this.end = end;
  11. this.sum = 0;
  12. }
  13. }
  14.  
  15. private STreeNode buildSTree(int[] nums, int start, int end) {
  16. if (start > end)
  17. return null;
  18. else {
  19. STreeNode node = new STreeNode(start, end);
  20. if (start == end) {
  21. node.sum = nums[start];
  22. }
  23. else {
  24. int mid = start + (end - start) / 2;
  25. node.left = buildSTree(nums, start, mid);
  26. node.right = buildSTree(nums, mid+1, end);
  27. if (node.left != null && node.right != null)
  28. node.sum = node.left.sum + node.right.sum;
  29. }
  30. return node;
  31. }
  32. }
  33.  
  34. private STreeNode root;
  35. public NumArray(int[] nums) {
  36. root = buildSTree(nums, 0, nums.length-1);
  37. }
  38.  
  39. public void update(int i, int val) {
  40. update(root, i, val);
  41. }
  42.  
  43. private void update(STreeNode node, int pos, int val) {
  44. // if (node == null)
  45. // return;
  46. if (node.start == pos && node.end == pos) {
  47. node.sum = val;
  48. return;
  49. }
  50. if (node.left != null && node.right != null) {
  51. int mid = node.start + (node.end - node.start) / 2;
  52. if (pos <= mid)
  53. update(node.left, pos, val);
  54. else
  55. update(node.right, pos, val);
  56.  
  57. node.sum = node.left.sum + node.right.sum;
  58. }
  59.  
  60. }
  61.  
  62. public int sumRange(int i, int j) {
  63. return sumRange(root, i, j);
  64. }
  65.  
  66. private int sumRange(STreeNode node, int i, int j) {
  67. if (node == null)
  68. return 0;
  69. if (node.start == i && node.end == j)
  70. return node.sum;
  71. int mid = node.start + (node.end - node.start) / 2;
  72. if (j <= mid)
  73. return sumRange(node.left, i, j);
  74. else if (i > mid)
  75. return sumRange(node.right, i, j);
  76. else
  77. return sumRange(node.left, i, mid) + sumRange(node.right, mid+1, j);
  78. }
  79.  
  80. }
  81.  
  82. /**
  83. * Your NumArray object will be instantiated and called as such:
  84. * NumArray obj = new NumArray(nums);
  85. * obj.update(i,val);
  86. * int param_2 = obj.sumRange(i,j);
  87. */

LeetCode - 307. Range Sum Query - Mutable的更多相关文章

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

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

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

  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】307. Range Sum Query - Mutable

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

  6. [Leetcode Week16]Range Sum Query - Mutable

    Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...

  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

    问题描述:给定一序列,求任意区间(i, j)的元素和:修改任意一元素,实现快速更新 树状数组 树状数组的主要特点是生成一棵树,树的高度为logN.每一层的高度为k,分布在这一层的序列元素索引的二进制表 ...

  9. 【leetcode】307. Range Sum Query - Mutable

    题目如下: 解题思路:就三个字-线段树.这个题目是线段树用法最经典的场景. 代码如下: class NumArray(object): def __init__(self, nums): " ...

随机推荐

  1. 记node前后端代码共用的一次坑

    项目背景 nodejs项目,webpack打包,用axios请求,Promise封装,nunjucks模板引擎: 之前已将nunjucks模板通过webpack打包策略,做成前后端共用: 目前需要将网 ...

  2. php ueditor 后台配置项返回格式出错,上传功能将不能正常使用!

    解决常见的有两种 1,可能是时区设置问题,有系统区分大小写. date_default_timezone_set("Asia/chongqing");改为 date_default ...

  3. dede的pagelist标签的listsize数字属性详解

    转载▼http://blog.sina.com.cn/s/blog_a4f3bd4e01012c8n.html dede的pagelist标签的listsize数字属性详解.见远seo经常用织梦搭建各 ...

  4. 01 整合IDEA+Maven+SSM框架的高并发的商品秒杀项目之业务分析与DAO层

    作者:nnngu 项目源代码:https://github.com/nnngu/nguSeckill 这是一个整合IDEA+Maven+SSM框架的高并发的商品秒杀项目.我们将分为以下几篇文章来进行详 ...

  5. SQLite 链接大全

    http://www.cnblogs.com/stephen-liu74/archive/2012/01/22/2328757.html

  6. c指针作为参数传递以及指针的指针

    指针作为函数参数传递 函数参数传递的只能是数值,所以当指针作为函数参数传递时,传递的是指针的值,而不是地址. #include "stdio.h" void pointer(int ...

  7. MVVM探索:从ViewModel关闭Window的最佳实践

    在WPF里使用MVVM开发的时候,似乎总是不可避免的会遇到这样一个问题:ViewModel在处理完业务之后需要关闭这个Window,这时候要怎么处理? 网上有很多解决方案:有的在ViewModel抛出 ...

  8. linux_磁盘体系

    未曾习艺先学礼,未曾学武先习德 当今磁盘都是温室磁盘,原理是一样的,高速转动的的盘,磁头做径向运动 当今磁盘的发展趋势: 体积更小.速度更快.容量更大.使用更安全 速度更快: 主轴转速: 10000/ ...

  9. js 抛物线 笔记备份

    var funParabola = function(element, target, options) { /* * 网页模拟现实需要一个比例尺 * 如果按照1像素就是1米来算,显然不合适,因为页面 ...

  10. Can’t open /dev/* exclusively. Mounted filesystem?解决

    1 错误提示:Can’t open /dev/* exclusively. Mounted filesystem? 做完软件RAID之后,根据鸟哥书上的操作,其实没有真正删除软件RAID,导致出现如下 ...