Range Sum Query - Immutable(easy)
1.这道题目与pat中的1046. Shortest Distance (20)相类似;
2.使用一个数组dp[i],记录0到第i个数的和
3.求i到j之间的和时,输出dp[j]-dp[i]+num[i]即可。
AC代码如下:
class NumArray {
public:
vector<int> dp;
vector<int> num;
NumArray(vector<int> &nums) {
int n=nums.size();
dp=vector<int>(n,0);
num=nums;
for(int i=0;i<n;i++)
{
if(i>0)
dp[i]=dp[i-1]+nums[i];
else
dp[0]=nums[0];
}
}
int sumRange(int i, int j) {
return dp[j]-dp[i]+num[i];
}
};
// Your NumArray object will be instantiated and called as such:
// NumArray numArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);
Range Sum Query - Immutable(easy)的更多相关文章
- [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
303. Range Sum Query - Immutable Easy Given an integer array nums, find the sum of the elements betw ...
- [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 - 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算法题-Range Sum Query Immutable(Java实现)
这是悦乐书的第204次更新,第214篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第70题(顺位题号是303).给定整数数组nums,找到索引i和j(i≤j)之间的元素之 ...
- leetcode303 Range Sum Query - Immutable
""" Given an integer array nums, find the sum of the elements between indices i and j ...
- 【LeetCode】303. Range Sum Query - Immutable 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 保存累积和 日期 题目地址:https://leetcode. ...
- 【leetcode❤python】 303. Range Sum Query - Immutable
#-*- coding: UTF-8 -*- #Tags:dynamic programming,sumRange(i,j)=sum(j)-sum(i-1)class NumArray(object) ...
随机推荐
- template.js的介绍
artTemplate 介绍 artTemplate 是新一代 javascript 模板引擎,它采用预编译方式让性能有了质的飞跃,并且充分利用 javascript 引擎特性,使得其性能无论在前端还 ...
- 一键分享mob,方法二
2.快速生成项目http://wiki.mob.com/android-sharesdk完整的集成文档/ 修改目标项目名称和项目的包名: 由于直接复制jar包和资源的集成方式比较麻烦,ShareSDK ...
- ServletContext实现网站计数器
在网站开发中,有很多功能需要使用ServletContext,比如: 1.网站计数器 2.网站在线用户的显示 3.简单的聊天系统 总之,如果是涉及到不用用户共享数据,而这些数据量不大,同时又不希望写入 ...
- TiKV 在京东云对象存储元数据管理的实践
京东云对象存储是在 2016 年作为公有云对外公开的,主要特点是可靠.安全.海量.低成本,应用于包括一些常用的业务场景,比如京东内部的京东商城视频/图片云存储,面向京东云公有云外部的开发者的服务,和面 ...
- KVM---虚拟机网络管理
在上篇博客中我们完成了 KVM 虚机的安装,但是我发现虚机内的网络是不通的(当然了,在写这篇博客的时候已经把上篇博客中的配置文件修改好了,网络也是通的了,嘻嘻),所以这篇博客总结了一下虚机的网络连接方 ...
- PAT Advanced 1037 Magic Coupon (25) [贪⼼算法]
题目 The magic shop in Mars is ofering some magic coupons. Each coupon has an integer N printed on it, ...
- SYN洪泛(dos)攻击和DDOS攻击
在TCP三次握手中,服务器为了响应一个收到的SYN,分配并初始化连接变量和缓存,然后服务器发送一个SYNACK进行响应,并等待来自客户的ACK报文段,如果客户不发送ACK来完成该三次握手,最终,服务器 ...
- pandas读取和写入excel多个sheet表单
一.读取单个表单 import pandas as pd excel_reader=pd.ExcelFile('文件.xlsx') # 指定文件 sheet_names = excel_reader. ...
- win32概述
win32基于已有的框架 有意入口函数只有一个 都需要有一个主函数 所有程序的入口都是maincrtstartup tydedef 顾名思义 window是基于c,c++ 又想有自己所特有的数据类型 ...
- vue 中使用print.js 打印遇到的问题 ?
不管怎么设置打印部分的 margin和height 仍会在预览时多出一张空白页?求各位大佬遇到过的请留言谢谢!