标签:leetcode
https://oj.leetcode.com/problems/gas-station/
http://blog.csdn.net/linhuanmars/article/details/22706553
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int len = gas.length;
// Cost if run all stations
int allcost = 0;
// Start point
int start = 0;
// How many gas left
int left = 0;
for (int i = 0 ; i < len ; i ++)
{
allcost = allcost + gas[i] - cost[i];
left = left + gas[i] - cost[i];
if (left < 0)
{
start = i + 1;
left = 0;
}
}
if (start == len || allcost < 0)
return -1;
else
return start;
}
}标签:leetcode
原文地址:http://7371901.blog.51cto.com/7361901/1600752