303:区域和检索 - 数组不可变
This commit is contained in:
parent
e08428524d
commit
bb235fe769
80
src/main/java/leetcode/editor/cn/RangeSumQueryImmutable.java
Normal file
80
src/main/java/leetcode/editor/cn/RangeSumQueryImmutable.java
Normal 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)
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
<p>给定一个整数数组 <code>nums</code>,处理以下类型的多个查询:</p>
|
||||
|
||||
<ol>
|
||||
<li>计算索引 <code>left</code> 和 <code>right</code> (包含 <code>left</code> 和 <code>right</code>)之间的 <code>nums</code> 元素的 <strong>和</strong> ,其中 <code>left <= 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> 中索引 <code>left</code> 和 <code>right</code> 之间的元素的 <strong>总和</strong> ,包含 <code>left</code> 和 <code>right</code> 两点(也就是 <code>nums[left] + nums[left + 1] + ... + nums[right]</code> )</li>
|
||||
</ul>
|
||||
|
||||
<p> </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> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= i <= j < 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>
|
Loading…
Reference in New Issue
Block a user