题解 | #加油站#

加油站

http://www.nowcoder.com/practice/a013a0691a0343aeb262ca1450d2fe4e

双指针
有油时候前指针可以前进(烧油),没油时候后指针必须后退(取油),直到两指针碰上。
如果最终油量大于0,可以跑足一圈,否则不能。
class Solution:
    def gasStation(self , gas: List[int], cost: List[int]) -> int:
        # write code here
        sta,end=0,len(gas)-1
        theg=0
        while sta<=end:
            if theg<0:
                theg+=gas[end]-cost[end]
                end+=-1
            else:
                theg+=gas[sta]-cost[sta]
                sta+=1
        if theg>=0:
            return sta
        else:
            return -1

全部评论

相关推荐

07-15 18:09
门头沟学院 Java
点赞 评论 收藏
分享
06-12 17:46
门头沟学院 Java
运营你豪哥:来说重点: ​1.项目前置,时间倒序。​​ 2.​项目描述强化结果与量化效果(STAR原则里的R)。​​ ​3.个人技能精炼,明确掌握程度,突出核心。​​ ​4.增加强有力开头的个人总结部分。​​ 5.​优化教育背景(成绩排名)、合并奖项与活动。​​
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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