303. Range Sum Query - Immutable

2016-12-03  本文已影响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.

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

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

上一篇 下一篇

猜你喜欢

热点阅读