leetcode 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) -> -3
Note:
- You may assume that the array does not change.
- There are many calls to sumRange function.
刚开始看题,有些不懂,暴力破解后time exceed。很简单,聪明的人大概一会儿就想出来了。
class NumArray {
vector<int> sumArray;
public:
NumArray(vector<int> &nums) {
int temp=;
for(int i=;i<nums.size();i++){
temp+=nums[i];
sumArray.push_back(temp);
}
}
int sumRange(int i, int j) {
int result;
result=this->sumArray[j]-this->sumArray[i-];
return result;
}
};
// Your NumArray object will be instantiated and called as such:
// NumArray numArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);
题外话:
最近状态有些不好,我事实上是一个常常状态不好的人,我【容易受环境干扰】、【理性感性参半】、【不容易坚守自己】
因为上述的缺点,常常陷入责怪自己的状态中,却不知道如何改变,最差却一直使用的解决办法就是明天再说,也许明天就好了。呸。
leetcode range sum query的更多相关文章
- [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- LeetCode Range Sum Query 2D - Mutable
原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...
- Leetcode: Range Sum Query 2D - Mutable && Summary: Binary Indexed Tree
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query - Immutable & Range Sum Query 2D - Immutable
Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indice ...
- [LeetCode] Range Sum Query 2D - Immutable
Very similar to Range Sum Query - Immutable, but we now need to compute a 2d accunulated-sum. In fac ...
- Leetcode: Range Sum Query - Mutable && Summary: Segment Tree
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] Range Sum Query - Immutable
The idea is fairly straightforward: create an array accu that stores the accumulated sum fornums suc ...
随机推荐
- 使用base64提升视觉效果体验
最近在做一个微信端的小项目,前端代码写完后,就放在手机端测试,没什么问题,但是页面在加载和渲染时的效果却让人有些不爽,虽然是个小项目,我大可不必做这些,但是看着页面的闪动,就忍不住想做些什么. 先说说 ...
- Chrome浏览器扩展开发系列之五:Page Action类型的Chrome浏览器扩展
Page Action类型的Google Chrome浏览器扩展程序,通常也会有一个图标,但这个图标位于Chrome浏览器的地址栏内右端.而且这个图标并非始终出现,而是当某指定的页面打开时才会出现.也 ...
- JAVA中Socket的用法模拟服务端和客户端
<看透springMvc源代码分析与实践>学习笔记 Socket分为ServerSocket和Socket两个大类 ServerSocket用于服务端,可以通过accept方法监听请求,监 ...
- PHP+NGINX
1. 下载php编译包/nginx编译包(建议先装nginx再装php, php编译包我用的是5.5.35) 2. 创建好安装目录(我的编译包放在/home下) mkdir -p /usr/local ...
- c++调用python系列(1): 结构体作为入参及返回结构体
最近在打算用python作测试用例以便对游戏服务器进行功能测试以及压力测试; 因为服务器是用c++写的,采用的TCP协议,当前的架构是打算用python构造结构体,传送给c++层进行socket发送给 ...
- 在Linux下安装Oracle12c
其实,对于oracle数据库和oracle实例的安装,借用图形化安装还是比较容易的,只是有个别地方需要特别注意外,其余的默认安装即可: 1.安装前的准备: 启动SSH工具: 先启动倒数第三个(想用图像 ...
- 如何开始使用bootstrap
登陆Bootstrap官网:http://getbootstrap.com/ Bootstrap中的JS插件依赖于jQuery,因此jQuery要在Bootstrap之前引用 bootstrap框架初 ...
- luogu P3373 【模板】线段树 2
题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含三个整数N.M.P,分别 ...
- Spring Ioc-依赖注入的几种方式
一 setter方法注入 配置文件如下: <bean id="helloAction" class="org.yoo.action.SpringSetterHell ...
- HTML5 开发APP( 支付宝支付)
我们搞app有一个重要的功能那就是支付功能.无论是微信支付,支付宝,还是银联支付总要有一样支付手段来支持我们网上付款.实现完整的功能.我们公司app的支付方式经过大家开会讨论选择了支付宝支付(其实是当 ...