leetcode 134. Gas Station
来源:互联网 发布:nginx根据域名跳转 编辑:程序博客网 时间:2024/04/30 11:36
从start出发,最远期望能够到end,sum表示当前油量。
1、如果sum < 0,说明从start开始到此处 无法继续前进,则 start递减,企图这样来增加到达此处的剩余油量从而继续前进。
2、如果sum >= 0, 说明还可以进一步向前进发,end递增。
class Solution {public:int canCompleteCircuit(vector<int>& gas, vector<int>& cost){int start = gas.size() - 1;int end = 0;int sum = gas[start] - cost[start];while (start > end){if (sum >= 0){sum += gas[end] - cost[end];end++;}else{start--;sum += gas[start] - cost[start];}}return sum >= 0 ? start : -1;}};
0 0
- [LeetCode]134.Gas Station
- LeetCode 134. Gas Station
- [Leetcode] 134. Gas Station
- [leetcode] 134. Gas Station
- Leetcode-134.Gas Station
- 134. Gas Station LeetCode
- Leetcode 134. Gas Station
- [LeetCode]134. Gas Station
- LeetCode 134. Gas Station
- leetcode 134. Gas Station
- LeetCode *** 134. Gas Station
- LeetCode 134. Gas Station
- 【LeetCode】134. Gas Station
- LeetCode - 134. Gas Station
- LeetCode 134. Gas Station
- LeetCode 134. Gas Station
- [leetcode] 134. Gas Station
- leetcode-134. Gas Station
- 排序
- Collection
- php面试题(1)
- cdrtools生成&刻录2gb镜像时间
- Aanroid 加载so库失败:java.lang.UnsatisfiedLinkError
- leetcode 134. Gas Station
- Eclipse调试过程中,如何修改变量的值?
- Sklearn-preprocessing.scale/StandardScaler/MinMaxScaler
- JavaScript事件
- 使用Coded UI Test测试DataGridView遇到的问题
- Linux驱动技术(七) _内核定时器与延迟工作
- What is the difference between pages and blocks?
- Java移位运算符及小技巧
- Zepto Click点透情况解决办法