LeetCode 303 Range Sum Query-Immutable (Range Sum Query-permanent )(*)

Source: Internet
Author: User

LeetCode 303 Range Sum Query-Immutable (Range Sum Query-permanent )(*)
Translation

Given an integer array nums, find out the sum of all elements between index I to j (I is less than or equal to j) (including I and j. For example, given nums = [-, 3,-,-1] sumRange (0, 2)-> 1 sumRange (2, 5)->-1 sumRange (0, 5)->-3 annotation: you can assume that the array will not change. There are many calls to the sumRange function.
Original
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) -> 1sumRange(2, 5) -> -1sumRange(0, 5) -> -3Note:You may assume that the array does not change.There are many calls to sumRange function.
Analysis

At first, I thought this question was simple. I wrote the following code:

class NumArray {public:    vector
  
    numArray;    NumArray(vector
   
     &nums) {        numArray = nums;    }    int sumRange(int i, int j) {        int sum = 0;        while (i < j) {            sum += numArray[i++];        }        return sum;    }};
   
  

Because I don't know what the constructor is doing, it is useless to add such a sentence. The function is implemented, but the test cases with abnormal questions still collapsed.

As for how many abnormalities are involved in the test case of the question, the data is used to speak: A total of 214513 characters, including nums data and function calls. The nums length is not counted, however, the sumRange function can be called up to 10000 times.

When you see this test case, you will understand that you need to optimize the function call. A good way is to set all the returned values:

The sum of all elements from 0 to 1, the sum of all elements from 0 to 2, and the sum of all elements from 0 to 10000 ,.................. When calculating the sum of all elements between I and j (because I and j are included ), that is, the sum of all elements from 0 to j minus all elements from 0 to I-1.

So the code is as follows, but the time still uses the 624ms timeout.

class NumArray {public:    map
  
    rangeSum;    NumArray(vector
   
     &nums) {        int sum = 0;        for (int i = 0; i < nums.size(); ++i) {            sum += nums[i];            rangeSum.insert(pair
    
     (i, sum));        }    }    int sumRange(int i, int j) {        return rangeSum[j] - rangeSum[i - 1];    }};
    
   
  

Although I don't know much about the underlying implementation, because of the index, I consciously use vector to search for its value and to add the value more quickly, because it is only a value instead of a key-value pair.

The Code is as follows:

Code
class NumArray {public:    vector
  
    rangeSum;    NumArray(vector
   
     &nums) {        int sum = 0;        for (int i = 0; i < nums.size(); ++i) {            sum += nums[i];            rangeSum.push_back(sum);        }    }    int sumRange(int i, int j) {        return rangeSum[j] - rangeSum[i - 1];    }};
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.