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. [wordpress]WordPress地址(URL)错误,修改解决方案

    本人在修改Wordpress地址(URL)时,误操作使URL指向错误,后台无法进入. 解决方案 1.先利用Putty登陆到自己的服务器上(这里登陆方法我不再赘述): 2.登陆MySqL,并输入密码: ...

  2. Java 常用知识点

    Java 常用知识点 1.日期格式化 SimpleDateFormat Date date=new Date(System.currentTimeMillis()) ; SimpleDateForma ...

  3. 微信tinker 热修复

    Tinker 是微信官方的Android热补丁解决方案,它支持动态下发代码.So库以及资源,让应用能够在不需要重新安装的情况下实现更新.当然,你也可以使用Tinker来更新你的插件. github:h ...

  4. document.querySelectorAll() 兼容 IE6

    不多说,直接上代码 // 使用 css 选择器获取元素对象 兼容性封装 Test Already. function getElementsByCss(cssStr){ if(document.que ...

  5. [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离

    On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...

  6. HDFS的WEB页面访问常见问题

    HDFS的WEB UI管理页面 50070 端口 无法访问解决办法! 本文基于HADOOP-3..1.0,Cecntos7.0环境下进行测试,所以遇到很多新鲜的问题: 特别注意:HaDoop3.0之前 ...

  7. linux sed 用法

    目录 Overview 命令行选项 Command-Line Options manual http://www.gnu.org/software/sed/manual/sed.html Overvi ...

  8. Dear ImGUI 使用指南

    文档 1)Dear IMGui 2) 知乎 组件 1) Com 如何设置动态字符串? //char*pTest = "aaaa\0bbbb\0cccc\0dddd\0eeee\0\0&quo ...

  9. python 函数enumerate(x,y)的用法

    enumerate(x,y)函数是把元组tuple.字符串str.列表list里面的元素遍历和索引组合,其用法与range()函数很相似, 下面示例enumerate(x,y)用法以及range(x) ...

  10. Python全栈-JavaScript】jQuery工具

    jQuery工具 一.jQuery.browser.version 显示当前 IE 浏览器版本号. if ( $.browser.msie ) alert( $.browser.version ); ...