TreeviewCopyright © aleen42 all right reserved, powered by aleen42
303. 区域和检索 - 数组不可变
Java
/*
* @Author: Goog Tech
* @Date: 2020-09-15 15:07:30
* @LastEditTime: 2020-09-15 15:11:43
* @Description: https://leetcode-cn.com/problems/range-sum-query-immutable/
* @FilePath: \leetcode-googtech\#303. Range Sum Query - Immutable\Solution.java
* @Reference: https://leetcode-cn.com/problems/range-sum-query-immutable/solution/marveljian-dan-de-xue-xi-bi-ji-303-by-tyanyonecanc/
* @WebSite: https://algorithm.show/
*/
class NumArray {
private int[] sum;
// DP : 动态规划
public NumArray(int[] nums) {
sum = Arrays.copyOf(nums, nums.length);
for(int i = 1; i < sum.length; i++) {
// sum[i] 的值为 sum[0] 到 sum[i] 的所有值之和
sum[i] += sum[i - 1];
}
}
public int sumRange(int i, int j) {
return (i == 0) ? sum[j] : (sum[j] - sum[i - 1]);
}
}
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(i,j);
*/
Python
'''
Author: Goog Tech
Date: 2020-09-15 15:07:34
LastEditTime: 2020-09-15 15:08:56
Description: https://leetcode-cn.com/problems/range-sum-query-immutable/
FilePath: \leetcode-googtech\#303. Range Sum Query - Immutable\Solution.py
Reference: https://leetcode-cn.com/problems/range-sum-query-immutable/solution/marveljian-dan-de-xue-xi-bi-ji-303-by-tyanyonecanc/
WebSite: https://algorithm.show/
'''
class NumArray(object):
# DP : 动态规划
def __init__(self, nums):
"""
:type nums: List[int]
"""
if not nums: return
self.result = [0] * len(nums)
self.result[0] = nums[0]
for i in range(1, len(nums)):
# result[i] 的值为 nums[0] 到 nums[i] 的所有值之和
self.result[i] = self.result[i - 1] + nums[i]
def sumRange(self, i, j):
"""
:type i: int
:type j: int
:rtype: int
"""
if i == 0: return self.result[j]
else: return self.result[j] - self.result[i - 1]
# Your NumArray object will be instantiated and called as such:
# obj = NumArray(nums)
# param_1 = obj.sumRange(i,j)