作者: 负雪明烛
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. Linux—yum的python版本错误——初级解决方案

    为了安装rrdtool,发现不是少这个就是少那个,最后发现yum也不能用. 从网上找的解决yum问题. 转自:http://doarthon.blog.51cto.com/3175384/728809 ...

  2. 网站性能调优实战-学相伴KuangStudy

    面对并发我们是如何优化KuangStudy网站性能的? 每个项目都会随着用户和数据的增长调整架构,来面对未来的问题,我们也不例外,在1月5号我们平台正式公测后,引起了很多观众的热烈反响,仅仅4天,注册 ...

  3. 单元测试在Unity中的应用

    项目描述:简单演示单元测试在Unity中的应用 项目地址:UnityTestRunner_Tutorial - SouthBegonia 项目版本:2020.3.20f1 项目用法:打开就用,代码都放 ...

  4. [源码解析] PyTorch 分布式 Autograd (6) ---- 引擎(下)

    [源码解析] PyTtorch 分布式 Autograd (6) ---- 引擎(下) 目录 [源码解析] PyTtorch 分布式 Autograd (6) ---- 引擎(下) 0x00 摘要 0 ...

  5. HDFS04 HDFS的读写流程

    HDFS的读写流程(面试重点) 目录 HDFS的读写流程(面试重点) HDFS写数据流程 网络拓扑-节点距离计算 机架感知(副本存储节点的选择) HDFS的读数据流程 HDFS写数据流程 客服端把D: ...

  6. MyBatis 如何实现流式查询

    基本概念 流式查询指的是查询成功后不是返回一个集合而是返回一个迭代器,应用每次从迭代器取一条查询结果.流式查询的好处是能够降低内存使用. 如果没有流式查询,我们想要从数据库取 1000 万条记录而又没 ...

  7. oracle(数据备份)

    1 --oracle数据备份(三种方法) 2 --1.逻辑备份与恢复:用Oracle提供的工具,导入/导出(exp,imp),数据 3 --泵导入/导出(impdp,expdp),装入器(SQL*Lo ...

  8. 【Linux】【Basis】【Kernel】Linux常见系统调用

    一,进程控制 1)getpid,getppid--获取进程识别号 #include <sys/types.h> #include <unistd.h> pid_t getpid ...

  9. malloc() vs new

    Following are the differences between malloc() and operator new. (1)new calls constructors, while ma ...

  10. my38_MySQL事务知识点零记

    从innodb中查看事务信息 show engine innodb status\G; ------------ TRANSACTIONS------------Trx id counter 3153 ...