[LeetCode]--303. Range Sum Query - Immutable

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:
You may assume that the array does not change.
There are many calls to sumRange function.

我最开始用的就是把数组的元素相加,因为知道i,j嘛,可以依次相加,但是这样就会超时。

如果最开始在构造函数里面构造数组的时候就把和都存进去,这样既可。

public class NumArray {

    public int[] nums;

    public NumArray(int[] nums) {
        if (nums.length == 0)
            return;
        this.nums = new int[nums.length];
        Arrays.fill(this.nums, 0);
        this.nums[0] = nums[0];
        for (int i = 1; i < nums.length; i++) {
            this.nums[i] = this.nums[i - 1] + nums[i];
        }
    }

    public int sumRange(int i, int j) {
        if (this.nums.length == 0)
            return 0;
        if (j > this.nums.length - 1)
            j = this.nums.length - 1;
        if (i == 0)
            return this.nums[j];
        return this.nums[j] - this.nums[i - 1];
    }
}
时间: 2024-09-17 20:14:26

[LeetCode]--303. Range Sum Query - Immutable的相关文章

[LeetCode] Range Sum Query - Immutable

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: You may assume that the array do

[LeetCode] Design Excel Sum Formula 设计Excel表格求和公式

Your task is to design the basic function of Excel and implement the function of sum formula. Specifically, you need to implement the following functions: Excel(int H, char W): This is the constructor. The inputs represents the height and width of th

c语言-求大神帮忙 C语言 LeetCode的 Two Sum问题

问题描述 求大神帮忙 C语言 LeetCode的 Two Sum问题 求大神帮忙.我run时显示Runtime Error,不知道问题在哪里.. 还有,我也不理解注释中的: * Note: The returned array must be malloced, assume caller calls free(). 这句是什么意思 题目: Given an array of integers, find two numbers such that they add up to a specif

[LeetCode]40.Combination Sum II

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

[LeetCode] Smallest Range 最小的范围

You have k lists of sorted integers in ascending order. Find the smallest range that includes at least one number from each of the k lists. We define the range [a,b] is smaller than range [c,d] if b-a < d-c or a < c if b-a == d-c. Example 1: Input:[

LeetCode之K sum problem

做过leetcode的人都知道, 里面有2sum, 3sum(closest), 4sum等问题, 这些也是面试里面经典的问题, 考察是否能够合理利用排序这个性质, 一步一步得到高效的算法. 经过总结, 本人觉得这些问题都可以使用一个通用的K sum求和问题加以概括消化, 这里我们先直接给出K Sum的问题描述和算法(递归解法), 然后将这个一般性的方法套用到具体的K, 比如leetcode中的2Sum, 3Sum, 4Sum问题. 同时我们也给出另一种哈希算法的讨论. leetcode求和问题

[LeetCode]113.Path Sum II

[题目] Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] [分析] 题目要求求出所有路径

[LeetCode]39.Combination Sum

[题目] Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target)

[LeetCode]1.Two Sum

[题目] Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note