Siukwan Program

http://siukwan.sinaapp.com/

Range Sum Query - Immutable(easy)

1.这道题目与pat中的1046. Shortest Distance (20)相类似；

2.使用一个数组dp[i],记录0到第i个数的和

3.求i到j之间的和时，输出dp[j]-dp[i]+num[i]即可。

AC代码如下：

class NumArray {
public:

vector<int> dp;
vector<int> num;
NumArray(vector<int> &nums) {
int n=nums.size();
dp=vector<int>(n,0);
num=nums;
for(int i=0;i<n;i++)
{
if(i>0)
dp[i]=dp[i-1]+nums[i];
else
dp[0]=nums[0];
}
}

int sumRange(int i, int j) {
return dp[j]-dp[i]+num[i];
}
};

// Your NumArray object will be instantiated and called as such:
// NumArray numArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);

leetcode笔记：Range Sum Query - Mutable

2016-01-21 01:06:07

[leetcode-307]Range Sum Query - Mutable(java)

2015-11-22 16:22:01

(LeetCode 307) Range Sum Query - Mutable(树状数组讲解)

2016-04-17 15:45:04

LeetCode（303）Range Sum Query - Immutable

2015-12-04 13:31:02

LeetCode（307） Range Sum Query - Mutable

2015-12-12 17:21:23

leetcode -- Range Sum Query - Mutable -- 重点，有线段树

2016-01-14 10:35:09

leetcode笔记：Range Sum Query - Immutable

2016-01-20 23:45:27

Range Sum Query - Immutable

2015-11-10 20:00:52

[LeetCode] Range Sum Query - Immutable

2015-11-10 15:09:10

LeetCode303——Range Sum Query - Immutable

2015-11-17 23:01:30