题解 | #加油站#

加油站

https://www.nowcoder.com/practice/3b1abd8ba2e54452b6e18b31780b3635


#
# 
# @param gas int整型一维数组 
# @param cost int整型一维数组 
# @return int整型
#
class Solution:
    def canCompleteCircuit(self , gas , cost ):
        # write code here
        i=0
        while(i<len(gas)):
            next=(i+1)%len(gas)
            curOil=gas[i]-cost[i]
            while(next!=i and next<len(gas) and curOil>=0):
                curOil = curOil+gas[next]-cost[next]
                if curOil<0:
                    break
                next=(next+1)%len(gas)
            if curOil>=0:
                return i
            i += 1
        return -1

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-01 11:27
点赞 评论 收藏
分享
深夜书店vv:腾讯是这样的,去年很多走廊都加桌子当工区
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-04 14:23
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务