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 {

 public:
vector<int> Nums; NumArray(vector<int> nums) {
Nums =nums;
} int sumRange(int i, int j) {
int res = ;
for(int k = i;k<=j;k++)
res+=Nums[k];
return res;
}
};

法二:

sumRange(i,j)=sum[j+1]−sum[i]

 class NumArray {

 public:
vector<int> Nums;
vector<int> Sums; NumArray(vector<int> nums) {
Nums =nums;
Sums = vector<int>(nums.size()+,);
for(int i = ;i<nums.size();i++)
Sums[i+]=Sums[i]+nums[i];
} int sumRange(int i, int j) {
int res = ;
res = Sums[j+]-Sums[i];
return res;
}
};

303. Range Sum Query - Immutable(动态规划)的更多相关文章

  1. 303. Range Sum Query - Immutable(动态规划)

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

  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 区域和检索 - 不可变

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

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

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

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

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

  6. Leetcode 303 Range Sum Query - Immutable

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

  7. 303. Range Sum Query - Immutable

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

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

  9. 【一天一道LeetCode】#303.Range Sum Query - Immutable

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

随机推荐

  1. String painter HDU - 2476 -区间DP

    HDU - 2476 思路:分解问题,先考虑从一个空串染色成 B串的最小花费 ,区间DP可以解决这个问题 具体的就是,当 str [ l ] = = str [ r ]时 dp [ L ] [ R ] ...

  2. java动态代理实现与原理详细分析

    关于Java中的动态代理,我们首先需要了解的是一种常用的设计模式--代理模式,而对于代理,根据创建代理类的时间点,又可以分为静态代理和动态代理. 一.代理模式    代理模式是常用的java设计模式, ...

  3. Windows应用程序组成及编程步骤

    Windows应用程序组成及编程步骤: 1.应用程序的组成:一个完整的Windows应用程序通常由五种类型的文件组成 1.C语言源程序文件 2.头文件 3.模块定义文件 4.资源描述文件 5.项目文件 ...

  4. BZOJ.4942.[NOI2017]整数(分块)

    BZOJ 洛谷 UOJ 可能是退役之前最后一个BZOJ rank1了? 参考这里. 如果没有减法,对一个二进制数暴力进位,均摊复杂度是\(O(1)\)的(要进\(O(n)\)次位就至少需要\(O(n) ...

  5. K个排序链表的合并(Hard)

    问题来源:选自leetCode 23:合并K个排序链表 问题描述: 题目给定信息: 不确定需要合并的链表的数目,但依然要求我们把给定的这些有序链表合并成一个链表,并且保证合并的链表依然是有序的. 问题 ...

  6. [LeetCode] Random Point in Non-overlapping Rectangles 非重叠矩形中的随机点

    Given a list of non-overlapping axis-aligned rectangles rects, write a function pick which randomly ...

  7. ORACLE PL/SQL编程:把触发器说透

    本篇主要内容如下: 8.1 触发器类型 8.1.1 DML触发器 8.1.2 替代触发器 8.1.3 系统触发器 8.2 创建触发器 8.2.1 触发器触发次序 8.2.2 创建DML触发器 8.2. ...

  8. 实现promise

    // 判断变量否为function  const isFunction = variable => typeof variable === 'function'  // 定义Promise的三种 ...

  9. Django集成Bootstrap美化后台

    1.pip install bootstrap-admin 2.编辑项目下的settings.py,增加下面行,放在最前面 3.编辑项目下的settings.py,增加下面行

  10. MFC 修改标题

    1. Overwrite CMainFrame::PreCreateWindow. Clear the style FWS_ADDTOTITLE cs.style &= ~(LONG)FWS_ ...