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. 手机号验证正则表达式+Demo(亲测完毕)

    以下为本人亲测过的验证手机号格式的demo,需要的小伙伴拿走不谢~<!DOCTYPE html><html><head><meta charset=" ...

  2. php实现多进程和关闭进程

    一.php实现多进程 PHP有个pcntl_fork的函数可以实现多进程,但要加载pcntl拓展,而且只有在linux下才能编译这个拓展. 先代码: <?php$arr = ['30000000 ...

  3. VB中Winsock连续发送出现接收不到的异常问题解决方法

    VB里面用WINSOCK进行一对多连接的TCP连接时,经常需要群发消息给所有已连接的客户端.代码类似如下: Option Explicit Dim bytMsg() As Byte Private S ...

  4. Egret--设置全屏,控制浏览器全屏

    1, 手机浏览器打开的项目的时候,浏览器的虚拟按键/标题栏, 使得即便设置全屏也没有变成全屏(好像JS 中有方法向浏览器请求全屏) 2, 加载资源, 关闭后卸载, 再次进入游戏依然很快.不过登陆游戏的 ...

  5. php+ajax文件上传

    php+ajax文件上传 html: <input id="user_real_name" class="input_show" type="t ...

  6. PHP 常用设计模式 (转载)

    1.单例模式 单例模式顾名思义,就是只有一个实例.作为对象的创建模式, 单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例模式的要点有三个: 一是某个类只能有一个实例: ...

  7. javascript的数组之map()

    map()方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的回调函数后返回的结果.新数组 // ES6 let numbers = [1, 5, 10, 15]; let doubles ...

  8. Mac学习

    碰到问题可以多查看帮助文件 快速上手 1,自动隐藏顶端菜单栏: 通用-> 外观 menu bar 2,docker 程序坞,左边为应用程序,右边是文件或者文件夹,38线 3,option -&g ...

  9. python中一个py文件如何调用其他py文件中的类和函数

    HelloWorld  文件名称  Hello是类 from HelloWorld import Hello >>> h = Hello() >>> h.hello ...

  10. 查看历史会话等待事件对应的session信息

    此处以enq: TX - row lock contention等待时间为例. 查看snap_id对应时间 select to_char(s.startup_time,'dd Mon "at ...