classSolution(object):defcanCompleteCircuit(self,gas,cost):""" :type gas: List[int] :type cost: List[int] :rtype: int """sum=0# total remained gassubsum=0# remained gas for each periodindex=0foriinrange(len(gas)):ifsubsum+gas[i]-cost[i]>=0:# can come to next stationsubsum+=gas[i]-cost[i]sum+=gas[i]-cost[i]else:subsum=0index=i+1# recount from next stationsum+=gas[i]-cost[i]# still the totalifsum<0:# total gas can not cover a circlereturn-1else:returnindex
近期评论