Range Sum Query - Immutable
2015-11-10 20:00
323 查看
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
Example:
[/code]
Example:
Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3
public class NumArray { private int[] dp; public NumArray(int[] nums) { dp = new int[nums.length]; int sum = 0; for (int i = 0; i < nums.length; i++) { sum += nums[i]; dp[i] = sum; } } public int sumRange(int i, int j) { return i == 0 ? dp[j] : dp[j] - dp[i - 1]; } }
[/code]
相关文章推荐
- UI - UISwitchAndUIActivityIndicatorView
- 移动标签(marquee)属性详解
- 移动标签(marquee)属性详解
- 例题4-3 UVa 133 The Dole Queue
- String, StringBuffer, StringBuilder
- 《iOS Human Interface Guidelines》——Branding
- iOS8 对于NSURLRequest 的bug
- 《iOS Human Interface Guidelines》——Animation
- iOS开发笔记之问题记录——UIViewController
- POJ-2785 4 Values whose Sum is 0(折半枚举 sort + 二分)
- How Did Watson Answer? —— Factoid Question Decomposition
- cloudstack GuestNetwork Ingress-Egress rule
- UITextField实现UITextView监控值变化的功能textViewDidChange
- UIAlertView上添加输入框UITextField
- UGUI - 长按事件实现
- iOS 9界面适配利器:详解Xcode 7的新特性UIStackView
- LCS(Longest Common Subsequence 最长公共子序列)
- 77.下拉刷新MJRefresh和UITableView的section headerView冲突的原因分析与解决方案
- Android 腾讯入门教程( 智能手表UI设计 和 MVC模式 )
- Android开发UI之响应系统UI变化