# 0134. Gas Station ###### tags: `Leetcode` `Medium` `TikTok` Link: https://leetcode.com/problems/gas-station/ ## 思路 需要数学证明的一道题 要证明出如果从A开始到不了B,那么从A和B当中任何一个点开始都到不了B 所以只需要遍历一遍,如果发现中途currGas<0,那就将下一个点设为起始点 判断究竟能不能绕一圈:totalgas-totalcost>=0 ## Code ```java= class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { int gasStation = 0; int totalGas = 0; int currGas = 0; for(int i = 0;i < gas.length;i++){ totalGas += gas[i]-cost[i]; currGas += gas[i]-cost[i]; if(currGas<0){ gasStation = i+1; currGas = 0; } } return totalGas>=0?gasStation:-1; } } ```