力扣 495. 提莫攻击 C++
判断下一次中毒时间是否在持续时间内即可,如果在就只累加时间差timeSeries[i + 1] - timeSeries[i],否则就累加duration for循环在len-1时结束即可,因为最后一次中毒一定会累加duration的时间, 所以最后返回sum + duration
int findPoisonedDuration(vector
int sum = 0;
int len = timeSeries.size();
for (int i = 0; i < len-1; ++i)
{
if (timeSeries[i + 1] - timeSeries[i] > duration)
sum += duration;
else
sum += timeSeries[i + 1] - timeSeries[i];
共有 0 条评论