一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

我的个人博客已创建,欢迎大家持续关注!

一天一道leetcode系列依旧在csdn上继续更新,除此系列以外的文章均迁移至我的个人博客

另外,本系列文章已整理并上传至gitbook,网址:点我进

欢迎转载,转载请注明出处!

(一)题目

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

Note:

  • You may assume that the array does not change.
  • There are many calls to sumRange function.

(二)解题

题目大意:给定一个数组,计算它的区间和

解题思路:本题给出了NumArray的构造函数,区间和应该再构造函数内就已经计算好。

class NumArray {
public:
    NumArray(vector<int> &nums) {
        int size = nums.size();
        int sum = 0;
        for(int i = 0 ; i < size ; i++){
            vec.push_back(sum);//vec里面存放第0~i-1位数的和。
            sum+=nums[i];
        }
        vec.push_back(sum);//第0~i位的和需要压入vector
    }

    int sumRange(int i, int j) {
        return vec[j+1] - vec[i];//直接计算区间和
    }
public:
    vector<int> vec;
};
// Your NumArray object will be instantiated and called as such:
// NumArray numArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);

【一天一道LeetCode】#303.Range Sum Query - Immutable的更多相关文章

  1. [LeetCode] 303. Range Sum Query - Immutable (Easy)

    303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...

  2. [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 ...

  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

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

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

  6. LeetCode 303. Range Sum Query - Immutable (C++)

    题目: Given an integer array nums, find the sum of the elements between indices iand j (i ≤ j), inclus ...

  7. LeetCode 303 Range Sum Query - Immutable(范围总和查询-永久不变)(*)

    翻译 给定一个整型数组nums,找出在索引i到j(i小于等于j)之间(包含i和j)的全部元素之和. 比如: 给定nums = [-2,0,3,-5,2,-1] sumRange(0, 2) -> ...

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

  9. LeetCode #303. Range Sum Query

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

随机推荐

  1. [JSOI2007]文本生成器

    题目描述 JSOI交给队员ZYX一个任务,编制一个称之为“文本生成器”的电脑软件:该软件的使用者是一些低幼人群,他们现在使用的是GW文本生成器v6版. 该软件可以随机生成一些文章―――总是生成一篇长度 ...

  2. [Codeforces]856C - Eleventh Birthday

    题目大意:给出n个数,问有多少种排列把数字接起来是11的倍数.(n<=2000) 做法:一个数后面接一个数等同于乘上10的若干次幂然后加上这个数,10模11等于-1,所以10的若干次幂是-1或1 ...

  3. 中断下半部处理之tasklet

    1.tasklet概述 下半部和退后执行的工作,软中断的使用只在那些执行频率很高和连续性要求很高的情况下才需要.在大多数情况下,为了控制一个寻常的硬件设备,tasklet机制都是实现自己下半部的最佳选 ...

  4. POJ 3171 Cleaning Shifts

    Description Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. Th ...

  5. Lua和C#调用探秘

    转载请标明出处:http://www.cnblogs.com/zblade/ 在实际的项目中,大部分业务逻辑 程序员只需要负责lua层编写逻辑即可,或者在c#层添加一些静态函数,供lua层调用.那么对 ...

  6. Windows下设置 ssh key,配置GitHub ssh key

    1.新建一个目录,利用git工具打开 Git Bash Here 2.执行如下命令 ssh-keygen -t rsa -C "email@email.com" 其中邮箱为GitH ...

  7. spring基本原理

    作者:王奕然链接:https://www.zhihu.com/question/21346206/answer/101789659来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  8. avalon,xmp

  9. git报错:'fatal:remote origin already exists'怎么处理?附上git常用操作以及说明。

    git添加远程库的时候有可能出现如下的错误, 怎么解决? 只要两步: 1.先删除 $ git remote rm origin 2.再次执行添加就可以了. ---------------------- ...

  10. TP中的AJAX返回ajaxReturn()

    系统支持任何的AJAX类库,Action类提供了ajaxReturn方法用于AJAX调用后返回数据给客户端.并且支持JSON.XML和EVAL三种方式给客户端接受数据,通过配置DEFAULT_AJAX ...