题目

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

Note:

You may assume that the array does not change.

There are many calls to sumRange function.

分析

给定一个整数序列,求指定子序列和。

提示:数组不会发生变化;大量sumRange函数调用。

题目本身非常简单,只需要遍历 i 到 j ,累计得到和即可。但是,这样是TLE的,所给提示也就没有意义了。

所以,题目考察的是效能,换一个方向思考,我们可以存储子序列和,每个下标处的值为[0,i]的所有元素和;

那么[i,j]子序列和 =sum[j]−sum[i−1];

注意,i==0时,直接返回sum[j]即可。

AC代码

class NumArray {
public:
NumArray(vector<int> &nums) {
if (nums.empty())
return ;
else
{
sums.push_back(nums[0]);
//求得给定数列长度
int len = nums.size();
for (int i = 1; i < len; ++i)
{
sums.push_back(sums[i - 1] + nums[i]);
}//for
}
} //计算[i,j]序列和
int sumRange(int i, int j) {
if (0 == i)
return sums[j];
int len = sums.size(); if (i < 0 || i >= len || j < 0 || j >= len || i > j)
{
return 0;
}//if
return sums[j] - sums[i-1];
} private:
//存储数列和
vector<int> sums;
};

GitHub测试程序源码

LeetCode(303)Range Sum Query - Immutable的更多相关文章

  1. LeetCode(304)Range Sum Query 2D - Immutable

    题目 Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...

  2. LeetCode(307) Range Sum Query - Mutable

    题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...

  3. leetcode@ [303/304] Range Sum Query - Immutable / Range Sum Query 2D - Immutable

    https://leetcode.com/problems/range-sum-query-immutable/ class NumArray { public: vector<int> ...

  4. [LeetCode] 303. Range Sum Query - Immutable (Easy)

    303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...

  5. [LeetCode] Range Sum Query - Immutable & Range Sum Query 2D - Immutable

    Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indice ...

  6. LeetCode_303. Range Sum Query - Immutable

    303. Range Sum Query - Immutable Easy Given an integer array nums, find the sum of the elements betw ...

  7. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  8. [LeetCode] 303. Range Sum Query - Immutable 区域和检索 - 不可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  9. [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

随机推荐

  1. Spring Security在标准登录表单中添加一个额外的字段

    概述 在本文中,我们将通过向标准登录表单添加额外字段来实现Spring Security的自定义身份验证方案. 我们将重点关注两种不同的方法,以展示框架的多功能性以及我们可以使用它的灵活方式. 我们的 ...

  2. 滚动条美化插件 nicescroll

    这个插件使用起来非常简单,首先下载插件jquery.nicescroll.min.js 然后在页面中引入jquery,再引入这个插件, 然后在页面中需要修改滚动条的地方,例如id为box的div &l ...

  3. 深入JVM内核---原理,诊断与优化

    JVM的概念 JAM是Java Virtual Machine的简称.意为Java虚拟机 虚拟机 指通过软件模拟的具有完整硬件系统功能的,运行在一种完整隔离环境中的完整计算机系统 有哪些虚拟机 - V ...

  4. android :fragmentation使用中遇到的 NullPointerException

    背景:fragmentation(单ACTIVITY+多个fragments)+brvah(  recyclerView多级自定义菜单功能) 目的:实现  菜单栏的点击,fragment 显示相应的内 ...

  5. wordpress安装后首页无法进入 The file 'wp-config.php' already exists

    问题是缓存导致,具体还没研究是怎么产生的缓存.chrome浏览器解决方法: 1. 网址后面加参数进入网站 2. 打开控制台-network 3. 刷新页面 4. 控制台-network,右键请求的文件 ...

  6. Intellij IDEA项目添加资源文件

    添加了一个资源文件,但读取的时候出错了 prop.load(Config.class.getResourceAsStream("/resources/dbconfig.properties& ...

  7. 以后要进行数据收集,打开邮箱就行了 | formtalk入驻Office 应用商店

    『数据收集』,作为一项工作,存在感高的忽视不了——不管你在企业里是什么角色(大部分),Ta似乎都在你的工作范围内. 你是人事:收集招聘数据.员工信息: 你是采购:收集供应商信息.商品数据: 你是市场: ...

  8. SQL Server 填充因子

    在创建聚集索引时,表中的数据按照索引列中的值的顺序存储在数据库的数据页中.在表中插入新的数据行或更改索引列中的值时,Microsoft®   SQL   Server™   2000   可能必须重新 ...

  9. C语言中字符串数组的遍历和比较

    /* The list of known types of default packet. */static char  *_default_packet_types[] = {    "d ...

  10. userBean之设置属性

    package com.java.model; public class Student { private String name;private int age; public String ge ...