134. Gas Station
2016-09-15 10:29
183 查看
这道题和剑指offer那个求最大和的题目有点类似,当之前的和为负的时候就可以舍弃,还有个小陷阱,全程的油量必须大于耗油,这是个圆圈跑道。
public class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { int shengyu=0; int begin=0; int total=0; int remainning=0; for(int i=0;i<gas.length;i++){ shengyu+=gas[i]; shengyu-=cost[i]; remainning = gas[i]-cost[i]; total += remainning; if(shengyu<0){ begin = i+1; shengyu=0; continue; }else{ continue; } } if(total>=0){ return begin; }else{ return -1; } } }
相关文章推荐
- [Leetcode] #134 Gas Station
- 134-Gas Station
- LeetCodeP134 Gas Station
- LeetCode(134) Gas Station
- 134. Gas Station
- 134. Gas Station
- Getting error "No subject alternative names" when doing secure URL connection(针对处理https连接的安全异常)(转:http://www.coderanch.com/t/134
- Codeforces Round 134 div 2 C题
- Android ApiDemos示例解析(134):Views->Layouts->Baseline->2. Bottom
- HDU1021---Fibonacci Again HDU(134)
- SGU 134 树的重心
- Gas Station
- LeetCode——Gas Station
- 134_leetcode_LRU Cache
- Gas Station (leetcode)
- Leetcode#134 Gas station
- LeetCode 134 Gas Station
- 给EditText的drawableRight属性的图片设置点击事件 分类: 学习笔记 android 2015-07-06 13:20 134人阅读 评论(0) 收藏
- 【LeetCode-面试算法经典-Java实现】【134-Gas Station(加油站问题)】
- 134 Gas Station [Leetcode]