leetcode 303 : Range Sum Query - Immutable

原创 2015年11月20日 11:22:05

1、原题如下:
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.

2、解题如下:

class NumArray {
public:
    vector<int> nums_accumulation_copy;
    NumArray(vector<int> &nums) {
        nums_accumulation_copy=nums;
        for(int a=1;a<nums.size();a++)
        {           nums_accumulation_copy[a]+=nums_accumulation_copy[a-1];
        }
    }

    int sumRange(int i, int j) {
        return (i==0)?nums_accumulation_copy[j]:(nums_accumulation_copy[j]-nums_accumulation_copy[i-1]);
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode-303. Range Sum Query - Immutable

本来真的是超简单一道题,但是数据量一大却变得不好处理。这道题也让我明白了在数据量不同的情况下完成相同的任务是完全不同的难度。303.Range Sum Query - Immutable Give...

leetcode 303. Range Sum Query - Immutable

leetcode 303

【LeetCode-303】Range Sum Query - Immutable(c++)

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

Leetcode 303 Range Sum Query - Immutable Python题解

题目大意输入一个数组,给定两个数组的索引i和j(i <= j),计算从索引i到j的和。 原题链接:303. Range Sum Query - Immutable题解一开始的时候我打算在初始化的时候...

LeetCode 303. Range Sum Query - Immutable

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

动态规划——303. Range Sum Query - Immutable[easy]

题目描述 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j),...
  • xiegr
  • xiegr
  • 2017-05-06 19:31
  • 57

303-e-Range Sum Query - Immutable

求数组中指定区间的和。 乍一看觉得题目莫名其妙,为啥还要指定数据结构呢,直接用系统数组求和不就行了么。写了个简单片段提交后直接超时,原来是会leet会大量调用你写的函数来求和而且区间往往很大,所以此...

303. Range Sum Query - Immutable [easy] (Python)

题目链接https://leetcode.com/problems/range-sum-query-immutable/题目原文 Given an integer array nums, find...

Leetcode 303. Range Sum Query - Immutable & 307. Range Sum Query - Mutable

303. Range Sum Query - Immutable Total Accepted: 30685 Total Submissions: 124438 Difficulty: Easy...

LeetCode 304. Range Sum Query 2D – Immutable

304. Range Sum Query 2D – Immutable Given a 2D matrix matrix, find the sum of the elements insi...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)