不理解这道题为什么是medium= =,挺简单的,乏善可陈。
我的解法
class Solution {
public:
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
int output = 0;
if (timeSeries.size() > 0){
for (int i = 1; i < timeSeries.size(); i ++){
output += timeSeries[i] - timeSeries[i-1] >= duration ? duration : timeSeries[i] - timeSeries[i-1] ;
}
output += duration;
}
return output;
}
};
他人解法
窃以为没自己好。
lass Solution {
public:
int findPosisonedDuration(vector<int>& ts, int duration) {
int tp = 0, pe = 0;
for(int i=0; i<ts.size(); ++i){
tp += duration - (ts[i] < pe) *(pe - ts[i]);
pe = ts[i] + duration;
}
return tp;
}
};
网友评论