每日AC--gas-station--LeetCode
2017-12-06 10:29
489 查看
每日AC--gas-station--LeetCode
题目描述
There are N gas stations along a circular route, where the amount of gas at station i isgas[i].You have a car with an unlimited gas tank and it costscost[i]of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.
Note:
The solution is guaranteed to be unique.
AC代码
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int len = gas.length;
for(int i = 0; i < len; i++){
if(canComplete(i,gas,cost)){
return i;
}
}
return -1;
}
public boolean canComplete(int start, int[] gas, int[] cost){
int len = gas.length;
int total = 0;
for (int i = start; i < len + start; i++) {
if (total + gas[i % len] < cost[i%len]) {
return false;
} else {
total = total + gas[i % len] - cost[i%len];
}
}
return true;
}
}
相关文章推荐
- LeetCode - 774. Minimize Max Distance to Gas Station
- leetcode gas station
- [LeetCode] Gas Station 加油站问题
- LeetCode之Gas Station --环形加油站周游
- LeetCode-Gas Station
- 开始刷题leetcode day5: Gas Station
- [LeetCode] Gas Station
- LeetCode 18 Gas Station
- leetcode:Gas Station
- Leetcode: Gas Station
- 【Leetcode】Gas Station
- [Leetcode]-Gas Station
- LeetCode——gas station
- [leetcode]Gas Station
- Leetcode_gas-station
- 【Leetcode】【Medium】Gas Station
- Leetcode gasstation
- Leetcode之Gas Station
- leetcode 134: Gas Station
- LeetCode | Gas Station