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) ...
随机推荐
- docker_jenkins
docker search jenkins docker pull jenkins 启动脚本 #!/bin/bash docker run -d --name myjenkins \ -u root ...
- 洛谷P1257(暴力超时)
1.先输入再求勾股定理会超时 2.需要一边输入一边求. #include<iostream> #include<cmath>#include<cstdio> usi ...
- POJ 1837:Balance 天平DP。。。
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11878 Accepted: 7417 Descript ...
- JAVA 算法练习(二)
和上次一样,虽说用 java 语言,但有 c 的基础一样可以看懂哦. 机器人走方格问题Ⅰ 题目概述 有一个XxY的网格,一个机器人只能走格点且只能向右或向下走,要从左上角走到右下角.请设计一个算法,计 ...
- CentOS系统安装过程中配置软RAID-0或RAID-1
什么是RAID-0 RAID-0 (等量模式, stripe):效能最佳.这种模式如果使用相同型号与容量的磁碟来组成时,效果较佳.这种模式的 RAID 会将磁碟先切出等量的区块 (举例来说, 4KB) ...
- gitlab命令详解
http://www.ruanyifeng.com/blog/2014/06/git_remote.html
- Anaconda Installation on Mac: conda command not found 环境变量配置
Mac系统安装完Anaconda 3.7后在terminal输入conda --version,返回command not found 原因可能是没有配置环境变量 在terminal输入vi ~/.b ...
- 新手学习Web前端的三个高效学习方法,基础要重视
作为新手,出于对风险的担心,不免在学习一项新技能或者转投一个新行业的时候,有所犹豫与徘徊.毕竟,在这场类似冒险的选择中,我们需要投入时间.精力以及承受相关的经济损失.但是,只有勇敢迈出第一步,才能为生 ...
- 用FFmpeg+nginx+rtmp搭建环境实现推流
Windows: 1.下载文件: 链接:https://pan.baidu.com/s/1c2LmIHHw-dwLOlRN6iTIMg 提取码:g7sj 2.解压文件: 解压到nginx-1.7.11 ...
- Linux下切换用户出现su: Authentication failure的解决办法
在切换用户时,密码没有输错,但始终无法成功地切换,还报出身份验证失败的错误,下面是具体解决方案: 在终端上输入指令sudo passwd root 此时输入你的密码 重复再次输入你的密码 再次用su指 ...