很经典的一道题也是很有意思的题目,网上的参考文章很多,思路就不给出了,直接上代码吧
public static int solution2_1_21(int[] gas,int[] cost){
int start=0,tank=0,total=0;
for(int i=0;i<gas.length;i++){
tank+=gas[i]-cost[i];
total+=gas[i]-cost[i];
if(tank<0){
start=i+1;
tank=0;
}
}
return total<0?-1:start;
}