leetcode Gas Station
来源:互联网 发布:梦幻群侠传3优化版攻略 编辑:程序博客网 时间:2024/09/21 09:00
此题思路较为简单,即判断是否存在从某一点开始,在保证每经过一个加油站都加上所有油的前提下,汽车都保持行驶状态,直至行驶完一圈。
需要注意的是程序结束条件。
代码
class Solution {public: int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { int start = 0; int j = start; int gasAll = gas[j]; int size = gas.size(); int count = 0; while(true) { if(count==size) return start; else { if(gasAll>=cost[j]) { gasAll -= cost[j]; j = (j+1)%size; gasAll += gas[j]; count++; } else { start = (start+1)%size; if(start==0) return -1; j = start; gasAll = gas[j]; count = 0; } } } }};
0 0
- LeetCode:Gas Station
- [LeetCode] Gas Station
- [leetcode]Gas Station
- Leetcode: Gas Station
- LeetCode:Gas Station
- [LeetCode] Gas Station
- leetcode Gas Station
- [LeetCode]Gas Station
- LeetCode 之 Gas Station
- leetcode Gas Station
- 【Leetcode】Gas Station
- LeetCode | Gas Station
- LeetCode之Gas Station
- Leetcode: Gas Station
- LeetCode OJ:Gas Station
- [LeetCode]134.Gas Station
- [Leetcode] Gas Station (Java)
- gas station leetcode
- 第一篇。
- jdk与jre的区别
- hdu1075 字典树
- spring mvc和spring的区别
- java学习课堂笔记2
- leetcode Gas Station
- 怎么建立win7无线热点
- 杭电1233还是畅通工程
- spring事务管理
- hdu-oj 1302 The Snail
- [水]ZOJ1241
- HDU 3255 Farming
- 杭电 1213 How Many Tables
- hdu 4920矩阵乘法