加油站
转载别人的代码, 没看懂!
class Solution {
public:
/**
* @param gas: a vector of integers
* @param cost: a vector of integers
* @return: an integer
*/
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
// write your code here
int m=gas.size();
if (m!= cost.size())
return -1;
int current =0;
int start = 0, totalgas =0,totalcost =0;
for (int i = 0; i<m ; i++) {
current +=gas[i] -cost[i];
totalgas+=gas[i];
totalcost+=cost[i];
if (current < 0) {
start = i+1;
current = 0;
}
}
if (totalgas<totalcost)
return -1;
else
return start;
}
};