leetcode134

Posted AsenYang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode134相关的知识,希望对你有一定的参考价值。

class Solution {
public:
    inline int get_next(int idx, int size)
    {
        return idx == size-1 ? 0 : idx+1;
    }
    
    int aux(int idx, vector<int>& gas, vector<int>& cost)
    {
        int i = idx;
        int left = gas[i] - cost[i];
        if(left < 0)
            return -1;
        i = get_next(i, gas.size());
        while( i!= idx)
        {
            left = left + gas[i] - cost[i];
            if(left < 0)
                return -1;
            i = get_next(i, gas.size());
        }
        return idx;
    }
    
    
    
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int ret;
        int i = 0;
        for(; i<gas.size(); ++i)
        {
            if(gas[i] >= cost[i])
            {
                ret = aux(i, gas, cost);
                cout << ret << endl;
                if(ret == -1)
                    continue;
                else
                    break;
            }
        }
        return ret;
    }
};

 

以上是关于leetcode134的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 134 解析

Leetcode 134 Gas Station

[leetcode-134-Gas Station]

leetcode134

LeetCode-134-Gas Station

[LeetCode]题解(python):134-Gas Station