134 Gas Station

题目 134 Gas Station

这道题有一个需要利用的条件即是如果存在这样一个station则答案唯一。

class Solution:
    # @param {integer[]} gas
    # @param {integer[]} cost
    # @return {integer}
    def canCompleteCircuit(self, gas, cost):
        s, tmp_s, ans = 0, 0, 0
        for i in range(0, len(gas)):
            val = gas[i] - cost[i]
            s += val
            tmp_s += val
            if tmp_s < 0:
                ans = i + 1
                tmp_s = 0
        if s < 0:
            return -1
        else:
            return ans

 

posted @ 2015-07-13 14:40  dapanshe  阅读(137)  评论(0编辑  收藏  举报