- 直接遍历
function canCompleteCircuit(gas: number[], cost: number[]): number {
const oils: number[] = gas.map((value, index) => gas[index] - cost[index]),
{ length } = oils;
for (let i = 0; i < length; ++i) {
if (oils[i] >= 0) {
let j: number = (i + 1) % length,
times: number = length - 1,
currentOil: number = oils[i];
while (times > 0) {
currentOil = currentOil + oils[j];
if (currentOil < 0) {
break;
}
j = (j + 1) % length;
--times;
}
if (!times) {
return i;
}
}
}
return -1;
};