题解 | #加油站#

加油站

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

全部评论

相关推荐

点赞 评论 收藏
分享
09-06 10:38
井冈山大学 Java
offer哪里跑__:第一眼,有点面生 第二眼,原来是我的余生 哥不白看,哥把这个月实习工资都给你,这是哥实习一天一天辛苦攒的,虽然少,但不真
正在实习的你,几点下班
点赞 评论 收藏
分享
09-09 16:35
门头沟学院 Java
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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