-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chenzhengwei
committed
Jan 12, 2017
1 parent
223165d
commit fb7769d
Showing
2 changed files
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#303. Range Sum Query - Immutable | ||
[题目链接](https://leetcode.com/problems/range-sum-query-immutable/) | ||
```java | ||
public class NumArray { | ||
|
||
private int[] nums; | ||
public NumArray(int[] nums) { | ||
// 预处理 | ||
for (int i=1; i < nums.length; i++) { | ||
nums[i] += nums[i - 1]; | ||
} | ||
this.nums = nums; | ||
} | ||
|
||
public int sumRange(int i, int j) { | ||
if (i == 0) { | ||
return nums[j]; | ||
} | ||
return nums[j] - nums[i - 1]; | ||
} | ||
} | ||
``` |