博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] 303. Range Sum Query - Immutable
阅读量:6879 次
发布时间:2019-06-26

本文共 566 字,大约阅读时间需要 1 分钟。

Problem

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) -> 1

sumRange(2, 5) -> -1
sumRange(0, 5) -> -3

Solution

class NumArray {    public int[] sum;    public NumArray(int[] nums) {        if (nums == null || nums.length == 0) return;        sum = new int[nums.length+1];        for (int i = 1; i <= nums.length; i++) {            sum[i] = sum[i-1]+nums[i-1];        }    }        public int sumRange(int i, int j) {        return sum[j+1]-sum[i];    }}

转载地址:http://dcgfl.baihongyu.com/

你可能感兴趣的文章
创建windows服务
查看>>
HTML5 入门基础
查看>>
【转载】读懂IL代码就这么简单(二)
查看>>
C++文件操作(fstream)
查看>>
R语言学习路线图-转帖
查看>>
【导入导出】sqlldr 导入含有内嵌换行符的数据
查看>>
Linux中常用命令
查看>>
RDS最佳实践(四)—如何处理Mysql的子查询
查看>>
最大流:Dinic模板
查看>>
安卓开发中个人能力的进阶进程
查看>>
人工智能10年将有颠覆性改变
查看>>
探秘AOP实现原理
查看>>
单点登录(SSO)简介
查看>>
2018最新大数据学习路线分享
查看>>
利用SVG制作不规矩背景的链接导航
查看>>
Linux - 一次运行多个命令
查看>>
10.C# -- 函数参数,参数数组,值传递函数,引用传递函数,输出函数,无参函数...
查看>>
BT5设置ip地址
查看>>
Effective Java读后感
查看>>
我的友情链接
查看>>