(java)Range Sum Query - Immutable
2016-01-08 11:52
141 查看
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
Example:
Note:
You may assume that the array does not change.
There are many calls to sumRange function.
// Your NumArray object will be instantiated and called as such:
// NumArray numArray = new NumArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);
思路:根据他的调用,无非就是添加一个钩子方法
代人如下(已通过leetcode)
public class NumArray {
private int[]nums;
public NumArray(int[] nums) {
this.nums=nums;
}
public int sumRange(int i, int j) {
int sum=0;
for(int k=i;k<=j;k++) {
sum+=nums[k];
}
return sum;
}
}
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.
// Your NumArray object will be instantiated and called as such:
// NumArray numArray = new NumArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);
思路:根据他的调用,无非就是添加一个钩子方法
代人如下(已通过leetcode)
public class NumArray {
private int[]nums;
public NumArray(int[] nums) {
this.nums=nums;
}
public int sumRange(int i, int j) {
int sum=0;
for(int k=i;k<=j;k++) {
sum+=nums[k];
}
return sum;
}
}
相关文章推荐
- Android TextView 跑马灯效果 android:ellipsize="marquee"
- Win7 Ubuntu双系统,删除并格式化Ubuntu分区后,启动出现grub rescue的解决方法
- uinput的使用
- UITextField文本框详解
- 1.2.0 Quickstart: "Hello World"(快速开始:“你好世界!”)
- 关于CC2540/2541编译Performing Post-Build Action卡住的问题
- iOS UISearchBar 和 UISearchDisplayController的使用
- ios UITableView 滑动 headerView footerView 跟随问题
- UIImage图片处理
- 输出字典中的key和 value
- 年结Close Process的两支Request
- mysql导入报错Variable 'sql_notes' can't be set to the value of 'NULL'
- 显示和隐藏easyui combobox
- 响应式web设计之CSS3 Media Queries
- EasyUI验证扩展
- Sql server 数据库 int 和guid 两者的比较
- UILabel关于支持HTML格式 自动换行 行间距的封装
- UITextView 四周间距
- [Leetcode]Distinct Subsequences
- UITableViewCell的backgroundColor和backgroundView