[Leetcode]Gas Station
2015-02-08 10:43
344 查看
There are N gas stations along a circular route, where the amount of gas at station i is
You have a car with an unlimited gas tank and it costs
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.
具体分析可以参考 http://blog.csdn.net/linhuanmars/article/details/22706553
class Solution:
# @param gas, a list of integers
# @param cost, a list of integers
# @return an integer
def canCompleteCircuit(self, gas, cost):
if gas is None or len(gas) == 0 or cost is None or len(cost) == 0 or len(gas) != len(cost):
return -1
sumVal = total = 0; index = -1
for i in xrange(len(gas)):
sumVal += gas[i] - cost[i]
total += gas[i] - cost[i]
if sumVal < 0:
sumVal = 0
index = i
return index + 1 if total >= 0 else -1
gas[i].
You have a car with an unlimited gas tank and it costs
cost[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.
具体分析可以参考 http://blog.csdn.net/linhuanmars/article/details/22706553
class Solution:
# @param gas, a list of integers
# @param cost, a list of integers
# @return an integer
def canCompleteCircuit(self, gas, cost):
if gas is None or len(gas) == 0 or cost is None or len(cost) == 0 or len(gas) != len(cost):
return -1
sumVal = total = 0; index = -1
for i in xrange(len(gas)):
sumVal += gas[i] - cost[i]
total += gas[i] - cost[i]
if sumVal < 0:
sumVal = 0
index = i
return index + 1 if total >= 0 else -1
相关文章推荐
- [LeetCode]Gas Station
- Leetcode 134. Gas Station (Medium) (cpp)
- leetcode---gas-station---贪心
- gas station leetcode
- leetcode:Gas Station 【Java】
- leetcode -- Gas Station
- LeetCode Gas Station
- 【Leetcode】Gas Station #134
- LeetCode Gas Station 两个特性,两种方法完美解答-更新证明方法
- 【Leetcode】【Medium】Gas Station
- 【LeetCode练习题】Gas Station
- LeetCode134 Gas Station
- LeetCode Gas Station
- [Leetcode] gas station 气站
- LeetCode题解:Gas Station
- LeetCode134—Gas Station
- 【leetcode】Gas Station
- leetcode题目:gas-station 使用start/end方法的个人理解
- [LeetCode] Gas Station
- LeetCode 134. Gas Station(加油站)