zoukankan      html  css  js  c++  java
  • 303. Range Sum Query

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.

    Example:

    Given nums = [-2, 0, 3, -5, 2, -1]
    
    sumRange(0, 2) -> 1
    sumRange(2, 5) -> -1
    sumRange(0, 5) -> -3
    

    Note:

    1. You may assume that the array does not change.
    2. There are many calls to sumRange function.

    本题比较简单,代码如下:

     1 public class NumArray {
     2     int[] sum;
     3     public NumArray(int[] nums) {
     4         sum = new int[nums.length+1];
     5         for(int i=0;i<nums.length;i++){
     6             sum[i+1] = sum[i]+nums[i];
     7         }
     8     }
     9     
    10     public int sumRange(int i, int j) {
    11         return sum[j+1]-sum[i];
    12     }
    13 }
    14 
    15 /**
    16  * Your NumArray object will be instantiated and called as such:
    17  * NumArray obj = new NumArray(nums);
    18  * int param_1 = obj.sumRange(i,j);
    19  */
  • 相关阅读:
    apio2018题解
    ynoi2018
    hdu2036
    Morley's Theorem
    计算几何
    luogu1355 神秘大三角
    poj2398
    洛谷---小L和小K的NOIP考后放松赛
    LibreOJ β Round #7
    python3
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6494325.html
Copyright © 2011-2022 走看看