Leetcode - Range Sum Query - Mutable

My code:

public class NumArray {
    private int[] nums;
    private int[] st;
    public NumArray(int[] nums) {
        if (nums == null || nums.length == 0) {
            return;
        }
        this.nums = nums;
        int n = nums.length;
        int level = (int) (Math.ceil(Math.log(n) / Math.log(2)));
        int len = 2 * (int) Math.pow(2, level) - 1;
        st = new int[len];
        buildST(0, 0, n - 1, nums, st);
    }

    void update(int i, int val) {
        int diff = val - nums[i];
        nums[i] = val;
        update(0, diff, i, 0, nums.length - 1, st);
    }

    public int sumRange(int i, int j) {
        return getSum(0, 0, nums.length - 1, i, j, st);
    }
    
    private void update(int index, int diff, int i, int ss, int se, int[] st) {
        if (i < ss || i > se) {
            return;
        }
        else if (ss == se) {
            st[index] += diff;
            return;
        }
        else {
            int mid = ss + (se - ss) / 2;
            st[index] += diff;
            update(2 * index + 1, diff, i, ss, mid, st);
            update(2 * index + 2, diff, i, mid + 1, se, st);
        }
    }
    
    private int getSum(int index, int ss, int se, int qs, int qe, int[] st) {
        if (qs <= ss && qe >= se) {
            return st[index];
        }
        else if (qe < ss || qs > se) {
            return 0;
        }
        else {
            int mid = ss + (se - ss) / 2;
            return getSum(2 * index + 1, ss, mid, qs, qe, st) + getSum(2 * index + 2, mid + 1, se, qs, qe, st);
        }
    }
    
    private int buildST(int index, int ss, int se, int[] nums, int[] st) {
        if (ss == se) {
            st[index] = nums[ss];
            return st[index];
        }
        else {
            int mid = ss + (se - ss) / 2;
            st[index] = buildST(2 * index + 1, ss, mid, nums, st) + buildST(2 * index + 2, mid + 1, se, nums, st);
            return st[index];
        }
    }
    
    
}


// 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);

这道题目采用了 segment tree的思想。然后
时间复杂度,
get sum: O(log n)
update: O(log n)
空间复杂度:
O(n) 其实是 2 * n -1

算是一种比较的方法了。

还有种做法叫做:
binary indexed tree

My code:

public class NumArray {
    int[] nums;
    int[] BIT;
    public NumArray(int[] nums) {
        this.nums = nums;
        BIT = new int[nums.length + 1];
        for (int i = 0; i < nums.length; i++) {
            updateBIT(i, nums[i]);
        }
    }

    void update(int i, int val) {
        int diff = val - nums[i];
        nums[i] = val;
        updateBIT(i, diff);
    }

    public int sumRange(int i, int j) {
        if (i == 0) {
            return getSum(j);
        }
        else {
            return getSum(j) - getSum(i - 1);
        }
    }
    
    private void updateBIT(int i, int diff) {
        int index = i + 1;
        while (index < BIT.length) {
            BIT[index] += diff;
            index += (index & -index);
        }
    }
    
    private int getSum(int i) {
        int index = i + 1;
        int ans = 0;
        while (index > 0) {
            ans += BIT[index];
            index -= (index & -index);
        }
        
        return ans;
    }
}


// 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);

时间复杂度,
get sum: O(log n)
update: O(log n)
空间复杂度:
O(n) 其实是 n + 1

所以,综合比起来,binary index tree 具有两大优势:

  1. 实现起来更简单
  2. 占用的空间更小些,虽然在big O上是相等的。

Anyway, Good luck, Richardo! — 09/04/2016

    原文作者:Richardo92
    原文地址: https://www.jianshu.com/p/7490cb035dee#comments
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞