leet-code/src/main/java/leetcode/editor/cn/RangeSumQueryMutable.java

90 lines
2.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给你一个数组 nums ,请你完成两类查询,其中一类查询要求更新数组下标对应的值,另一类查询要求返回数组中某个范围内元素的总和。
//
// 实现 NumArray 类:
//
//
//
//
// NumArray(int[] nums) 用整数数组 nums 初始化对象
// void update(int index, int val) 将 nums[index] 的值更新为 val
// int sumRange(int left, int right) 返回子数组 nums[left, right] 的总和nums[left] +
//nums[left + 1], ..., nums[right]
//
//
//
//
// 示例:
//
//
//输入:
//["NumArray", "sumRange", "update", "sumRange"]
//[[[1, 3, 5]], [0, 2], [1, 2], [0, 2]]
//输出:
//[null, 9, null, 8]
//
//解释:
//NumArray numArray = new NumArray([1, 3, 5]);
//numArray.sumRange(0, 2); // 返回 9 sum([1,3,5]) = 9
//numArray.update(1, 2); // nums = [1,2,5]
//numArray.sumRange(0, 2); // 返回 8 sum([1,2,5]) = 8
//
//
//
//
// 提示:
//
//
// 1 <= nums.length <= 3 * 104
// -100 <= nums[i] <= 100
// 0 <= index < nums.length
// -100 <= val <= 100
// 0 <= left <= right < nums.length
// 最多调用 3 * 104 次 update 和 sumRange 方法
//
//
//
// Related Topics 树状数组 线段树
// 👍 273 👎 0
package leetcode.editor.cn;
//307:区域和检索 - 数组可修改
public class RangeSumQueryMutable{
public static void main(String[] args) {
//测试代码
// Solution solution = new RangeSumQueryMutable().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class NumArray {
private int[] nums;
public NumArray(int[] nums) {
this.nums = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
update(i, nums[i]);
}
}
public void update(int i, int val) {
this.nums[i] = val;
}
public int sumRange(int i, int j) {
int sum = 0;
for (; i <= j; i++) {
sum += this.nums[i];
}
return sum;
}
}
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* obj.update(index,val);
* int param_2 = obj.sumRange(left,right);
*/
//leetcode submit region end(Prohibit modification and deletion)
}