【leetcode题解】[E][52]303. Range Sum Query - Immutable
来源:互联网 发布:天子星软件 编辑:程序博客网 时间:2024/06/02 02:16
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
Example:
Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sumRange(2, 5) -> -1sumRange(0, 5) -> -3
Note:
- You may assume that the array does not change.
- There are many calls to sumRange function.
Subscribe to see which companies asked this question
就是实现数据结构的动态规划。分别用left和right保存从左、从右算起的和,然后用总和减去左右就是中间的
class NumArray(object): def __init__(self, nums): if nums == []: return self.nums = nums self.total = sum(nums) self.left = [0]*len(nums) self.right = [0]*len(nums) #self.left[0] = nums[0] for i in xrange(1,len(nums)): self.left[i] = self.left[i-1] + nums[i-1] #print i,self.left[i] for i in xrange(len(nums)-2,-1,-1): self.right[i] = self.right[i+1] + nums[i+1] #print i,self.right[i] """ initialize your data structure here. :type nums: List[int] """ def sumRange(self, i, j): #print self.left #print 'right',self.right return self.total - self.left[i] - self.right[j] """ sum of elements nums[i..j], inclusive. :type i: int :type j: int :rtype: int """# Your NumArray object will be instantiated and called as such:# numArray = NumArray(nums)# numArray.sumRange(0, 1)# numArray.sumRange(1, 2)
0 0
- 【leetcode题解】[E][52]303. Range Sum Query - Immutable
- Leetcode题解 303. Range Sum Query - Immutable
- LeetCode 题解(286): Range Sum Query - Immutable
- LeetCode题解:Range Sum Query - Immutable
- <LeetCode OJ> 303. Range Sum Query - Immutable
- leetcode 303. Range Sum Query - Immutable
- [leetcode] 303. Range Sum Query - Immutable
- 303. Range Sum Query - Immutable [Leetcode]
- 303. Range Sum Query - Immutable LeetCode
- [Leetcode]303. Range Sum Query - Immutable
- Leetcode:303. Range Sum Query - Immutable(JAVA)
- LeetCode 303. Range Sum Query - Immutable
- LeetCode.303.Range Sum Query - Immutable
- LeetCode *** 303. Range Sum Query - Immutable
- LeetCode 303. Range Sum Query - Immutable
- leetcode-303. Range Sum Query - Immutable
- LeetCode-303.Range Sum Query - Immutable
- Leetcode 303. Range Sum Query - Immutable
- HP 性能测试工具LoadRunner 12.00 中的新增功能详解:附下载地址
- leetcode32---Longest Valid Parentheses
- 如何处理 SQL Server 安装过程中的 COM+ 检查失败问题
- 奇怪的排序
- 前端面试题目搜集——理论知识篇
- 【leetcode题解】[E][52]303. Range Sum Query - Immutable
- 黑马程序员——集合中并发修改异常ConcurrentModificationException
- web前端收藏夹
- Java Socket编程
- SQL数据库有关备份和恢复的语句及操作
- 网页简单布局之结构与表现原则
- c#中this的作用
- 脚踏实地 志存高远——我的码农之路
- thinkphp中使用ajax实现无刷新分页