当前位置:网站首页>leetcode:370. 区间加法
leetcode:370. 区间加法
2022-06-29 20:51:00 【OceanStar的学习笔记】
题目来源
题目描述

class Solution {
public:
vector<int> getModifiedArray(int length, vector<vector<int>>& updates) {
}
};
题目解析
差分数组
要对一个区间多次增减,然后返回原数据变成什么样子时,用算法:差分数组最简单
class Solution {
class Difference{
std::vector<int> diff;
public:
explicit Difference(int length){
diff.resize(length);
for (int i = 0; i < length; ++i) {
diff[i] = 0;
}
}
void update(int i, int j, int val){
diff[i] += val;
if(j + 1 < diff.size()){
diff[j + 1] -= val;
}
}
std::vector<int> result(){
int N = diff.size();
std::vector<int> res(N, 0);
res[0] = diff[0];
for (int i = 1; i < N; ++i) {
// diff[i] = res[i] - res[i - 1]---> res[i] = diff[i] + res[i - 1]
res[i] = diff[i] + res[i - 1];
}
return res;
}
};
public:
vector<int> getModifiedArray(int length, vector<vector<int>>& updates) {
Difference d(length);
for(auto arr : updates){
d.update(arr[0], arr[1], arr[2]);
}
return d.result();
}
};
int main() {
vector<vector<int>> positions {
{
1, 3, 2}, {
2, 4, 3}, {
0, 2, -2}};
Solution a;
auto ans = a.getModifiedArray(5, positions);
for(auto i : ans){
printf("%d\t", i);
}
return 0;
}
边栏推荐
- At least 3 years for learning amplifier?
- 期末复习【微机原理】
- STL教程6-deque、stack、queue、list容器
- Withdrawal of user curve in qualified currency means loss
- Jupyter service installation and startup
- Comparable comparator writing & ClassCastException class conversion exception
- Application of VoIP push in overseas audio and video services
- 量子机器学习的基础和应用:一个简明文献综述
- Detailed description of gaussdb (DWS) complex and diverse resource load management methods
- Lexin interview process
猜你喜欢

Cmake development - Multi Directory Project

Information system project manager -- Chapter VII examination questions of project cost management over the years

MySQL JSON data types & functions
![Final review [microcomputer principle]](/img/79/8311a409113331e72f650a83351b46.png)
Final review [microcomputer principle]

习近平在湖北武汉考察时强调 把科技的命脉牢牢掌握在自己手中 不断提升我国发展独立性自主性安全性

分析影响导电滑环传输信号的因素

Liunx instruction

Uncover the secret! Pay attention to those machines under the membership system!
【云原生实战】KubeSphere实战——多租户系统实战

Coreldraw2022 new version v24.1.0.360 update
随机推荐
Win10 sets automatic dial-up networking task to realize automatic reconnection after startup and disconnection
时钟树综合(CTS)
How to use the configuration in thinkphp5
WPF 测量字符串显示大小
fastadmin后台设置单选按钮
2021 CCPC 哈尔滨 J. Local Minimum (思维题)
Tag based augmented reality using OpenCV
Wangedit rich text editor usage (detailed)
Mysql Json 数据类型&函数
"Operation and maintenance department has Xiao Deng" to review and analyze file and folder access rights
Three. JS development: drawing of thick lines
Advances in computational imaging
Analysis of the factors affecting the transmission signal of the conductive slip ring
如何审核 Active Directory 用户账户更改?
Common methods of string class
mapbox-gl开发教程(十二):加载面图层数据
解释PBR纹理贴图(texture-maps)
In depth good article | yolov5+deepsort multi-target tracking in-depth interpretation and testing (including source code)
What problems should be avoided when using the points mall games for marketing?
Detailed description of gaussdb (DWS) complex and diverse resource load management methods