[LeetCode 307] Range Sum Query - Mutable

本文介绍了一种使用二叉树实现的动态数组结构,能够在保持更新操作和区间求和操作复杂度为O(log n)的同时,实现数组元素的动态修改与区间和的快速查询。通过构建段树,每进行一次更新或查询操作,只需遍历对应段树节点,从而显著提升算法效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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) -> 9
update(1, 2)
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.
Solution:

assume update and sumRange function is distributed evenly, we need O(lgn) complexity for both functions.

Could use binary tree achieve this. Use segment tree to store, the node has left to right range, and the range sum. For update operation, need to find the node and update all node which range include the position. For sumRange operation, need to find the nodes which range could consist the sumrange you are looking for.


public class NumArray {

    class TreeNode {
        int start = 0;
        int end = 0;
        int sum = 0;
        TreeNode left = null;
        TreeNode right = null;
    }
    TreeNode root = null;
    public void init(int[] nums) {
        if(nums == null || nums.length == 0) return;
        this.root = buildTree(0, nums.length-1, nums);
    }
    public TreeNode buildTree(int start, int end, int[] data) {
        TreeNode t = new TreeNode();
        t.start = start;
        t.end = end;
        if(start == end) {
            t.sum = data[start];
            return t;
        }        
        int mid = start + (end-start)/2;
        t.left = buildTree(start, mid, data);
        t.right = buildTree(mid+1, end, data);
        t.sum = t.left.sum + t.right.sum;
        return t;
    }
    public NumArray(int[] nums) {
        init(nums);
    }

    public void updateTree(TreeNode node, int index, int val) {
        if(node == null) return;
        if(node.start == node.end) {
            node.sum = val;
            return;
        }
        int mid = node.start + (node.end - node.start)/2;
        if(index<=mid) {
            updateTree(node.left, index, val);
        }else {
            updateTree(node.right, index, val);
        }
        node.sum = node.left.sum + node.right.sum;
    }
    
    void update(int i, int val) {
        updateTree(this.root, i, val);
    }
    public int queryTree(TreeNode node, int left, int right) {
        if(node == null) return 0;
        if(node.start == left && node.end == right) return node.sum;
        int mid = node.start + (node.end - node.start)/2;
        if(mid>=right) {
            return queryTree(node.left, left, right);
        }else if(mid<left) {
            return queryTree(node.right, left, right);
        }else {
            return queryTree(node.left, left, mid) + queryTree(node.right, mid+1, right);
        }
    }
    public int sumRange(int i, int j) {
        return queryTree(root, i, j);
    }
}


// Your NumArray object will be instantiated and called as such:
// NumArray numArray = new NumArray(nums);
// numArray.sumRange(0, 1);
// numArray.update(1, 10);
// numArray.sumRange(1, 2);




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值