"""
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
"""
"""
两种做法,第一种动态规划
第二种切片
"""
class NumArray: def __init__(self, nums):
n = len(nums)
self.sum = [0]*(n+1) #!!!self.的使用
for i in range(1, n+1):
self.sum[i] = self.sum[i-1] + nums[i-1] #!!!动态规划方程
def sumRange(self, i, j):
return self.sum[j+1]-self.sum[i] class NumArray(object): def __init__(self, nums):
self.nums = nums #!!!换成self def sumRange(self, i, j):
return sum(self.nums[i:j + 1])

leetcode303 Range Sum Query - Immutable的更多相关文章

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

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

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

  3. LeetCode_303. Range Sum Query - Immutable

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

  4. [LeetCode] 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 区域和检索 - 不可变

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

  6. [Swift]LeetCode303. 区域和检索 - 数组不可变 | Range Sum Query - Immutable

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

  7. 【LeetCode】303. Range Sum Query - Immutable 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 保存累积和 日期 题目地址:https://leetcode. ...

  8. 【leetcode❤python】 303. Range Sum Query - Immutable

    #-*- coding: UTF-8 -*- #Tags:dynamic programming,sumRange(i,j)=sum(j)-sum(i-1)class NumArray(object) ...

  9. Leetcode 303 Range Sum Query - Immutable

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

随机推荐

  1. 吴裕雄--天生自然PythonDjangoWeb企业开发:解决Pythonno module named "XX"问题

    在项目中加入 sys.path.append('你的django项目路径') sys.path.append('python的site-packages路径')

  2. 一起探讨Go 语言为什么能成功?

    导读 两位创造者Rob Pike和Robert Griesemer一起探讨了Go成功的原因. 常言道,历史不会重演,但总会惊人的相似. 如果您想创建一种编程语言,多向那些有经验的人士学习,他们有很多可 ...

  3. MessageBox函数

    <Windows程序设计>(第五版)(美Charles Petzold著) https://docs.microsoft.com/zh-cn/windows/desktop/apiinde ...

  4. SpringBoot之基础入门-专题一

    SpringBoot之基础入门-专题一 一.Spring介绍 1.1.SpringBoot简介 在初次学习Spring整合各个第三方框架构建项目的时候,往往会有一大堆的XML文件的配置,众多的dtd或 ...

  5. Spring_MVC 21问

    1.什么是Spring MVC ?简单介绍下你对springMVC的理解? Spring MVC是一个基于Java的实现了MVC设计模式的请求驱动类型的轻量级Web框架,通过把Model,View,C ...

  6. FFmpeg调用c语言SDK实现日志的打印

    日志文件的三大步 // 导入头文件 #include <libavutil/log.h> // 设置日志级别 av_log_set_level(AV_LOG_DEBUG); //DEBUG ...

  7. 解决 /actuator/beans不能访问

    在Spring Boot中配置了actuator,能够访问到/actuator/health,但是在访问/actuator/beans的时候却显示如下404错误. 原因是 /actuator/heal ...

  8. python-python基础7

    一.静态方法 通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里可以通过self.调用实例变量或类 ...

  9. SQL常用关键字

    常用SQL语句 SAP实际上提供了两种访问数据库的方式:Open SQL与Native SQL ---语句 功能 数据操作   insert 向表添加新数据行 delete 从表中删除数据行 upda ...

  10. 使用input:file控件在微信内置浏览器上传文件返回未显示选择的文件

    使用input:file控件在微信内置浏览器上传文件返回未显示选择的文件 原来的写法: <input type="file" accept="image/x-png ...