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. 还不知道spring的RestTemplate的妙用吗

    为什么要使用RestTemplate? 随着微服务的广泛使用,在实际的开发中,客户端代码中调用RESTful接口也越来越常见.在系统的遗留代码中,你可能会看见有一些代码是使用HttpURLConnec ...

  2. Jquery中事件的重复绑定

    问题:事件的绑定具有叠加效果,已有绑定的函数时,再次绑定,执行时会出现绑定多少次,触发多少次的情况 解决:1.on();绑定事件,提供了绑定事件处理程序所需的所有功能,用于统一取代以前的bind(). ...

  3. RHEL5.8安装

    创建完成后新的虚拟机.使用光盘启动后,启动界面如下图. 大概介绍下显示界面内容的意思:     1.To install or upgrade in graphical mde, press the ...

  4. “扩展域”与"边带权"的并查集

    https://www.luogu.org/problemnew/show/P1196 银河英雄传说 #include<bits/stdc++.h> using namespace std ...

  5. 太原面经分享:如何用js实现返回斐波那契数列的第n个值的函数

    面试攒经验,let's go! 值此高考来临之际,闲不住的我又双叒叕出发去面试攒经验了,去了公司交待一番流程后,面试官甩给了我一张A4纸,上面写着一道js算法笔试题(一开始我并不知道这是在考察js算法 ...

  6. 2、vuex页面刷新数据不保留,解决方法(转)

    今天这个问题又跟页面的刷新有一定的关系,虽然说跟页面刷新的关系不大,但确实页面刷新引起的这一个问题. 场景: VueX里存储了 this.$store.state.PV这样一个变量,这个变量是在app ...

  7. Git飞行规则

    原文链接 Git飞行规则(Flight Rules)

  8. Linux学习笔记(三)

    昨天将计算机概论部分算是看完了,然后我又将后面的内容过了一遍,因为我本身对Linux有一些了解,所以就跳到这文件权限与目录管理配置这章了,话不多说,开始做笔记. Linux文件权限与目录配置 群组 所 ...

  9. 关于vue页面多了之后,webpack热更新速度慢的解决办法

    vue项目大了之后,每次热更新都要10多秒的时间, 网上找了一大堆发现一个有用的办法 "env": { "development":{ "plugin ...

  10. CentOS7安装MySQL并允许远程登录

    下载 mysql 源安装包 $ curl -LO http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm 安装 mys ...