美文网首页
303. Range Sum Query - Immutable

303. Range Sum Query - Immutable

作者: 我是你的果果呀 | 来源:发表于2016-12-03 09:11 被阅读0次

Given an integer arraynums, find the sum of the elements between indicesiandj(ij), inclusive.

Example:
Given nums = [-2, 0, 3, -5, 2, -1]
sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
umRange(0, 5) -> -3
Note:

You may assume that the array does not change.
There are many calls tosumRangefunction.

数组范围求和, 很简单吧, 如果每次循环加起来一定不是面试官想要的。他们要找聪明的人, 
什么是聪明人, 用简单的方法实现别人做起来复杂的事情!

开始就把前面的数字累加起来到当前的位置。这样求和一个减法足够了。

相关文章

网友评论

      本文标题:303. Range Sum Query - Immutable

      本文链接:https://www.haomeiwen.com/subject/zlxcmttx.html