Leetcode-303 Range Sum Query - Immutable(区域和检索 - 数组不可变)

 1 class NumArray
 2 {
 3     public:
 4         vector<int> input;
 5         vector<int> dp;
 6         NumArray(vector<int> nums)
 7         {
 8             input = nums;
 9             dp.push_back(nums[0]);
10             for(int i = 1;i < nums.size();i ++)
11             {
12                 dp.push_back(dp[i-1]+nums[i]);
13             }
14         }
15 
16         int sumRange(int i, int j)
17         {
18             return dp[j]-dp[i]+input[i];
19         }
20 };

 

posted @ 2018-09-01 11:45  Asurudo  阅读(165)  评论(0编辑  收藏  举报