C语言 | Leetcode C语言题解之第134题加油站-题解:

int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize) {
    int i = 0;
    while (i < gasSize) {
        int sumOfGas = 0, sumOfCost = 0;
        int cnt = 0;
        while (cnt < gasSize) {
            int j = (i + cnt) % gasSize;
            sumOfGas += gas[j];
            sumOfCost += cost[j];
            if (sumOfCost > sumOfGas) {
                break;
            }
            cnt++;
        }
        if (cnt == gasSize) {
            return i;
        } else {
            i = i + cnt + 1;
        }
    }
    return -1;
}
上一篇:Web前端指南


下一篇:【全开源】废品回收垃圾回收小程序APP公众号源码PHP版本