LeetCode #303. Range Sum Query
问题:
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) -> -3Note:
- You may assume that the array does not change.
- There are many calls to sumRange function.
题目大意:
自己构造一个类,构造函数传入一数组,求和函数给一个起始下标,一个终止下标,求这个闭区间的和。
初始思路:
类中有一个数组,构造函数把传入的数组赋值给类的数组。
求和时从起始下标开始遍历至终止下标。
class NumArray {
public:
NumArray(vector<int> &nums) {
arr = nums;
} int sumRange(int i, int j) {
int sum = 0;
while (i <= j)
{
sum += arr[i];
i++;
}
return sum;
}
private:
vector<int> arr;
};
这种思路中,复杂度为O(j-i)(O(n)),题目中说sum函数会频繁调用,复杂度不好,结果是超时。
改进思路:
因为频繁的使用sum函数,需要降低该函数的复杂度。降低的方法从构造函数入手,使得类内的数组存着sum值,这样可以得到复杂度O(1)的sum函数。
class NumArray {
public:
NumArray(vector<int> &nums) {
arr.push_back(0);
for (int i = 1; i<=nums.size(); i++)
arr.push_back(arr[i - 1] + nums[i-1]);
} int sumRange(int i, int j) {
return (arr[j + 1] - arr[i]);
}
vector<int> arr;
};
这段代码的关键在构造函数,这里类中的arr数组下标i存的是nums数组前i-1个元素的和。每次将前一个sum元素加num元素得到一个sum元素追加到尾部,相当于遍历了一遍num数组,构造函数的复杂度是O(n)。值得注意的是,采用一般的思路时,构造函数的复杂度也是O(n)。
总结
这道题看起来非常简单,即使初学者也可完成,但想要降低复杂度就需要一个巧妙的算法。
题目中自己编写的代码不知是一个求和函数,而是一个类(包括构造函数),这应该是一个很强的提示,要利用只调用一次的构造函数,去降低sum函数的复杂度。
题目另一个提示就是There are many calls to sumRange function.这也体现了需要降低sum函数复杂度的需求。
这是本人第一道leetcode题目,以后要加油啊~
LeetCode #303. Range Sum Query的更多相关文章
- [LeetCode] 303. Range Sum Query - Immutable (Easy)
303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...
- [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 ...
- 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 ...
- Leetcode 303 Range Sum Query - Immutable
题意:查询一个数组在(i,j]范围内的元素的和. 思路非常简单,做个预处理,打个表就好 拓展:可以使用树状数组来完成该统计,算法复杂度为(logn),该数据结构强力的地方是实现简单,而且能完成实时更新 ...
- 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 ...
- 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 ...
- LeetCode 303 Range Sum Query - Immutable(范围总和查询-永久不变)(*)
翻译 给定一个整型数组nums,找出在索引i到j(i小于等于j)之间(包含i和j)的全部元素之和. 比如: 给定nums = [-2,0,3,-5,2,-1] sumRange(0, 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 ...
- [LeetCode] 304. Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
随机推荐
- Web应用网络模型
Web应用网络模型 前言 这篇文章要介绍的是一个常见Web应用基本的过程跟网络模型,当然,对于多数的Client/Server应用也是适用的.延续这个系列文章的风格,只管通俗不管严谨. 概览 总体模型 ...
- Android 手机卫士4--设置中心显示
1,自定义属性 SettingActivity.java package com.itheima.mobilesafe74.activity; import com.itheima.mobilesaf ...
- ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint......
今天用Hibernate建立外键的时候发现没有建立 但是创建了这个字段 情景: user表有一字段role,role是role表id字段的外键 原因: user表中已经有记录了,而且有的记录role这 ...
- jQuery fullPage.js 全屏滚动
fullPage 是一款不依赖任何 js 库的全屏滚动组件,支持垂直/水平滚动.CSS3 旋转/缩放动画,支持 IE5.5+,支持移动设备. 在线实例 垂直滚动 水平滚动 CSS3 动画1 CSS3 ...
- 定制Eclipse IDE之功能篇(一)
上文回顾:定制Eclipse IDE之界面篇 这一篇文章将记录一些Eclipse插件拓展(extension),简单讲述常用拓展的用法,但可能不会那么详细. 我的主要插件的拓展如下: 一.or ...
- SQLServer表内自关联级联删除
今天处理SQLServer级联删除遇到了很蛋疼的事. SQLServer 不支持表内自关联级联删除,而MySql和Oracle却支持. 貌似原因是SQLServer 会产生循环级联,就不给这样弄.所以 ...
- CALayer 易混淆的两个属性 - position和anchorPoint
1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property CGPoint position; ...
- Java 静态语句块、语句块、构造函数执行顺序
class Parent{ static String name = "hello"; { System.out.println("3 parent block" ...
- App 即时通讯 SDK
1.网易云信 http://netease.im/ 2.环信 http://www.easemob.com/customer/im 3.融云 http://www.rongcloud.cn/ 4.极光 ...
- Android NDK之JNI陷阱
背景: 最近一个月一直在做移植库的工作,将c代码到share library移植到Android平台.这就涉及到Android NDK(native develop kit)内容.这里只想记录下JNI ...