博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Gas Station
阅读量:6854 次
发布时间:2019-06-26

本文共 912 字,大约阅读时间需要 3 分钟。

There are N gas stations along a circular route, where the amount of gas at station i is gas[i].

You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.

Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.

Note:

The solution is guaranteed to be unique.

超时代码O(n2):

class Solution{  public:      int canCompleteCircuit(vector
&gas, vector
&cost) { if(gas.empty()||cost.empty()) return -1; int myToalGas=0; int remain=0; for (int i=0;i
=0) continue; else break; } if(j==gas.size()) return i; } return -1; } };

 

转载于:https://www.cnblogs.com/fightformylife/p/4246125.html

你可能感兴趣的文章