作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/range-sum-query-immutable/description/

题目描述

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.

解题方法

保存累积和

可以直接用切片求和的方法做,也能A,但是效率太慢。

下面这个方式可以先把sums求出来,然后再调用的时候直接右边的sums-左边的sums即可得到结果。

class NumArray(object):

    def __init__(self, nums):
"""
:type nums: List[int]
"""
self.sums = [0] * len(nums)
total = 0
for i, num in enumerate(nums):
total += num
self.sums[i] = total def sumRange(self, i, j):
"""
:type i: int
:type j: int
:rtype: int
"""
if i == 0:
return self.sums[j]
else:
return self.sums[j] - self.sums[i - 1] # Your NumArray object will be instantiated and called as such:
# obj = NumArray(nums)
# param_1 = obj.sumRange(i,j)

如果多用一个元素放在开头,那么上面的这个代码可以简化。

class NumArray(object):

    def __init__(self, nums):
"""
:type nums: List[int]
"""
N = len(nums)
self.sums = [0] * (N + 1)
for i in range(1, N + 1):
self.sums[i] = self.sums[i - 1] + nums[i - 1] def sumRange(self, i, j):
"""
:type i: int
:type j: int
:rtype: int
"""
return self.sums[j + 1] - self.sums[i] # Your NumArray object will be instantiated and called as such:
# obj = NumArray(nums)
# param_1 = obj.sumRange(i,j)

日期

2018 年 2 月 4 日
2018 年 11 月 24 日 —— 周六快乐

【LeetCode】303. Range Sum Query - Immutable 解题报告(Python)的更多相关文章

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

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

  2. [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 ...

  3. Java [Leetcode 303]Range Sum Query - Immutable

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

  4. 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 ...

  5. Leetcode 303 Range Sum Query - Immutable

    题意:查询一个数组在(i,j]范围内的元素的和. 思路非常简单,做个预处理,打个表就好 拓展:可以使用树状数组来完成该统计,算法复杂度为(logn),该数据结构强力的地方是实现简单,而且能完成实时更新 ...

  6. LeetCode 303. Range Sum Query - Immutable (C++)

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

  7. LeetCode 303 Range Sum Query - Immutable(范围总和查询-永久不变)(*)

    翻译 给定一个整型数组nums,找出在索引i到j(i小于等于j)之间(包含i和j)的全部元素之和. 比如: 给定nums = [-2,0,3,-5,2,-1] sumRange(0, 2) -> ...

  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】#303.Range Sum Query - Immutable

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

随机推荐

  1. Generic recipe for data analysis with general linear model

    Generic recipe for data analysis with general linear model Courtesy of David Schneider State populat ...

  2. SSH客户端工具连接Linux(有的也可以连接Windows、mac、iOS等多系统平台)

    要远程操作Linux的话还是得靠SSH工具,一般来说,Linux是打开了默认22端口的SSH的服务端,如果我们要远程它的话,就需要一个SSH客户. 我对一款好用的工具主要需要满足以下几点. (1)连接 ...

  3. TCP三次握手与Linux的TCP内核参数优化

    感谢各位技术大佬的资料分享,这里我把我理解的内容做一个整理 一:TCP的三次握手 1.TCP简述 TCP是一个面向连接的协议,在连接双方发送数据之前,首先需要建立一条连接.TCP建立连接可以简单称为: ...

  4. Linux—find在指定路径下查找文件或目录

    find /指定路径  -name  "*filename*" find /指定路径  -name  "*filename*"  2>/dev/null  ...

  5. 一个好用的快速安装lnmp环境包lnmp1-6

    一个好用的快速安装lnmp环境包lnmp1-6 地址:https://lnmp.org/tag/lnmp1-6/

  6. Spark基础:(六)Spark SQL

    1.相关介绍 Datasets:一个 Dataset 是一个分布式的数据集合 Dataset 是在 Spark 1.6 中被添加的新接口, 它提供了 RDD 的优点(强类型化, 能够使用强大的 lam ...

  7. 【Swift】CoreData的使用

    CoreData只是iOS数据持久化的其中一个方法,所有数据持久化如下 1.plist文件(属性列表),通常用于储存用户设置,也zhi可以用于存储捆绑的信息: 2.preference(偏好设置),常 ...

  8. t01_docker安装TiDB

    Docker环境安装TiDB,在官方说明的基础上补充了几个细节,安装记录如下 个人环境-vbox上安装centos7.4系统 CPU:12核24线程,分配给虚拟机12线程 MEM: 48G,分配给虚拟 ...

  9. linux下把一个用户从某个组中删除,而不删除用户

    查看当前用户/登录用户 基本语法 whoami / who am I 用户组 介绍 类似于角色,系统可以对有共性的多个用户进行统一的管理. 新增组 语法 groupadd 组名 案例演示 添加test ...

  10. C# 使用modbus 读取PLC 寄存器地址

    使用的组件Nmodbus 定义参数,全局变量: //创建modbus实体对象 private static ModbusFactory modbusFactory; private static IM ...