解题思路:
滑动窗口提交代码:
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int curGas = 0,len=0;
int start = 0, end = 0;
while (start < gas.length) {
if(len==gas.length) return start;
curGas+=gas[end]-cost[end];
if (curGas < 0) {
start++;end=start;
curGas=0;len=0;
continue;
}
end++;len++;
if(end==gas.length)
end=0;
}
return -1;
}
}
运行结果:

本文深入探讨了滑动窗口算法的应用,通过一个具体的示例,详细讲解了如何使用滑动窗口解决环形加油站问题,包括代码实现和运行结果分析。
363

被折叠的 条评论
为什么被折叠?



