题解 | #加油站#

加油站

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-03 16:13
嘉应学院 Python
xiaolihuam...:很明显骗子,如果是hr直接约你面试了,哪用得着内推,如果是员工的话,你得多优秀,一线员工直接加你微信,
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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