"""
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. 解决HTML5(富文本内容)连续数字、字母不自动换行

    最近开发了一个与富文本相关的功能,大概描述一下:通过富文本编辑器添加的内容,通过input展示出来(这里用到了 Vue 的 v-html 指令). 也是巧合,编辑了一个只有数字组成的长文本,等到展示的 ...

  2. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 显示代码:变量赋值

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. The 2019 ICPC China Nanchang National Invitational and International Silk-Road Programming Contest - F.Sequence(打表+线段树)

    题意:给你一个长度为$n$的数组,定义函数$f(l,r)=a_{l} \oplus a_{l+1} \oplus...\oplus a_{r}$,$F(l,r)=f(l,l)\oplus f(l,l+ ...

  4. 建设基于TensorFlow的深度学习环境

    一.使用yum安装git 1.查看系统是否已经安装git git --version 2.yum 安装git yum install git 3.安装成功 git --version 4.进入指定目录 ...

  5. Windows的本地时间(LocalTime)、系统时间(SystemTime)、格林威治时间(UTC-Time)、文件时间(FileTime)之间的转换

    今天处理了一个Bug,创建历史数据时脚本函数的起始时间不赋值或者赋0值时,计算引擎推给历史库的UTC时间为-288000000000,一开始以为是bug,经过分析后发现不赋值默认给起始时间赋0值,而此 ...

  6. Windows 控制台命令笔记

    1. cmd中输出中文乱码问题: CHCP是一个计算机指令,能够显示或设置活动代码页编号. C:\windows\system32>CHCP 活动代码页: 936 原因是我们使用了GBK编码,下 ...

  7. Python 基础之循环结构for及break pass continue

    一.for 循环 #循环 变量 迭代 都是一个意思#把列表里面的元素意义的拿出来就是遍历listvar = ["one","two","three&q ...

  8. ubuntu 中怎么安装 jdk 7

    Jdk1.7 安装包的下载地址是: http://www.oracle.com/technetwork/java/javase/downloads/jdk-7u4-downloads-1591156. ...

  9. IOS 错误原因

    当xcode提示以下错误时,很可能的原因是由于ViewController中的View在Controller中连接了outlet,然后又删除了Controller中对应的属性,导致xcode找不到这个 ...

  10. python读取文件用b模式读取

    f = open('aaa','rb')    返回的是字节 字符串编码 python中所有的字符串编码为Unicode,如果从一个文件读取字符串,那么该字符串的编码就是该文件的编码. f.tell( ...