303:区域和检索 - 数组不可变

This commit is contained in:
轩辕龙儿 2022-04-11 17:31:52 +08:00
parent e08428524d
commit bb235fe769
2 changed files with 122 additions and 0 deletions

View File

@ -0,0 +1,80 @@
//给定一个整数数组 nums处理以下类型的多个查询:
//
//
// 计算索引 left right 包含 left right之间的 nums 元素的 其中 left <= right
//
//
// 实现 NumArray
//
//
// NumArray(int[] nums) 使用数组 nums 初始化对象
// int sumRange(int i, int j) 返回数组 nums 中索引 left right 之间的元素的 总和 包含 left
//right 两点也就是 nums[left] + nums[left + 1] + ... + nums[right] )
//
//
//
//
// 示例 1
//
//
//输入
//["NumArray", "sumRange", "sumRange", "sumRange"]
//[[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]
//输出
//[null, 1, -1, -3]
//
//解释
//NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);
//numArray.sumRange(0, 2); // return 1 ((-2) + 0 + 3)
//numArray.sumRange(2, 5); // return -1 (3 + (-5) + 2 + (-1))
//numArray.sumRange(0, 5); // return -3 ((-2) + 0 + 3 + (-5) + 2 + (-1))
//
//
//
//
// 提示
//
//
// 1 <= nums.length <= 10
// -10 <= nums[i] <= 10
// 0 <= i <= j < nums.length
// 最多调用 10 sumRange 方法
//
// Related Topics 设计 数组 前缀和 👍 443 👎 0
package leetcode.editor.cn;
//303:区域和检索 - 数组不可变
public class RangeSumQueryImmutable {
public static void main(String[] args) {
//Solution solution = new RangeSumQueryImmutable().new Solution();
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
class NumArray {
int[] nums;
int[] sums;
public NumArray(int[] nums) {
this.nums = nums;
sums = new int[nums.length + 1];
for (int i = 0; i < nums.length; i++) {
sums[i + 1] = sums[i] + nums[i];
}
}
public int sumRange(int left, int right) {
return sums[right + 1] - sums[left];
}
}
/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(left,right);
*/
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,42 @@
<p>给定一个整数数组 &nbsp;<code>nums</code>,处理以下类型的多个查询:</p>
<ol>
<li>计算索引&nbsp;<code>left</code>&nbsp;&nbsp;<code>right</code>&nbsp;(包含 <code>left</code><code>right</code>)之间的 <code>nums</code> 元素的 <strong></strong> ,其中&nbsp;<code>left &lt;= right</code></li>
</ol>
<p>实现 <code>NumArray</code> 类:</p>
<ul>
<li><code>NumArray(int[] nums)</code> 使用数组 <code>nums</code> 初始化对象</li>
<li><code>int sumRange(int i, int j)</code> 返回数组 <code>nums</code>&nbsp;中索引&nbsp;<code>left</code>&nbsp;&nbsp;<code>right</code>&nbsp;之间的元素的 <strong>总和</strong> ,包含&nbsp;<code>left</code>&nbsp;&nbsp;<code>right</code>&nbsp;两点(也就是&nbsp;<code>nums[left] + nums[left + 1] + ... + nums[right]</code>&nbsp;)</li>
</ul>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>
["NumArray", "sumRange", "sumRange", "sumRange"]
[[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]
<strong>输出:
</strong>[null, 1, -1, -3]
<strong>解释:</strong>
NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);
numArray.sumRange(0, 2); // return 1 ((-2) + 0 + 3)
numArray.sumRange(2, 5); // return -1 (3 + (-5) + 2 + (-1))
numArray.sumRange(0, 5); // return -3 ((-2) + 0 + 3 + (-5) + 2 + (-1))
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
<li><code>-10<sup>5</sup>&nbsp;&lt;= nums[i] &lt;=&nbsp;10<sup>5</sup></code></li>
<li><code>0 &lt;= i &lt;= j &lt; nums.length</code></li>
<li>最多调用 <code>10<sup>4</sup></code><code>sumRange</code><strong> </strong>方法</li>
</ul>
<div><div>Related Topics</div><div><li>设计</li><li>数组</li><li>前缀和</li></div></div><br><div><li>👍 443</li><li>👎 0</li></div>