495. Teemo Attacking
来源:互联网 发布:板块分析软件 编辑:程序博客网 时间:2024/05/19 02:20
分析题目之后发现本题的解题思路可以是先求出数组相邻两次毒药的时间差,如果这个差值比duration要大,则在总时间上加上duration的值。若差值小于等于duration,则意味着上一次毒药效果还没发挥完就又被毒了一次,因此上一次毒药的作用时间为两次毒药的时间差,即在总时间上加上这个时间差。根据此思路写出以下代码:
int findPoisonedDuration(vector<int>& timeSeries, int duration) { int total = 0; for(int i = 0; i < timeSeries.size() - 1; i ++){ int dif = timeSeries[i + 1] - timeSeries[i]; if(dif > duration)total += duration; else total += dif;}total += duration;return total;}但是提交结果为runtime error, last input 为 [ ], 100000.
发现之前没有考虑到数组为空的情况,于是加上一行判断数组是否为空:
int findPoisonedDuration(vector<int>& timeSeries, int duration) { int total = 0; if(timeSeries.size() == 0)return total; for(int i = 0; i < timeSeries.size() - 1; i ++){ int dif = timeSeries[i + 1] - timeSeries[i]; if(dif > duration)total += duration; else total += dif;}total += duration;return total;}Accepted
0 0
- 495. Teemo Attacking
- LeetCode 495. Teemo Attacking
- LeetCode 495. Teemo Attacking
- 495. Teemo Attacking
- 495. Teemo Attacking
- 495. Teemo Attacking
- LeetCode:495. Teemo Attacking
- 【Leetcode】495. Teemo Attacking
- 495. Teemo Attacking
- [Array]495. Teemo Attacking
- 495. Teemo Attacking
- [leetcode] 495. Teemo Attacking
- 495. Teemo Attacking
- 495. Teemo Attacking
- leetcode 495. Teemo Attacking
- 【LeetCode】495. Teemo Attacking
- LeetCode 495. Teemo Attacking
- 495. Teemo Attacking
- Python小程序
- Redis实战 PDF电子书人民邮电出版社
- oracle超出打开游标的最大数解决方法
- JFinal中Config的c3p0连接池语句
- No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
- 495. Teemo Attacking
- Linux常用操作指令
- Wolf and Rabbit
- EffectiveJava(6)消除过期对象的引用
- Ajax技术(XMLHttp)
- WIN10隐藏任务栏和桌面图标(简易实现)
- springMVC中web.xml配置/和/*的区别
- STM8L051之低功耗停机配置问题
- 思维万能体系——申论