加油站
1,如果汽油总量小于里程数,一定无法走完一圈,返回-1
2,如果汽油总量不小于里程数,这先局部判断,从当前汽油量不小于里程数的位置记起
3,如果从当前往后走直到最后仍有油,这一定能走完一圈,否则返回-1
class Solution(object):
def canCompleteCircuit(self, gas, cost):
"""
:type gas: List[int]
:type cost: List[int]
:rtype: int
"""
if sum(gas) < sum(cost):
return -1
k = -1
j = 0
for i in range(0, len(gas)):
j = j + gas[i] - cost[i]
if j >= 0 and k < 0:
k = i
if j < 0:
k = -1
j = 0
return k