## 一.題目描述
There are N gas stations along a circular route, where the amount of gas at station i is 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.
環形路線上有N個加油站,每個加油站有汽油`gas[i]`,從每個加油站到下一站消耗汽油`cost[i]`,問從哪個加油站出發能夠回到起始點,如果都不能則返回-1,最后題目提到解是唯一的。
## 二.題目分析
以下解法的時間復雜度為:`O(n)`,每個車站可加油量和每段距離的耗油量分別存放在`gas`和`cost`中,可以設置兩個變量:`sum`用于判斷當前是否有足夠的油量走到下一個加油站,若`sum<0`,則需將出發車站前移一個車站;`remainingGas`用于記錄車開完整個過程后的油量。最終,判斷`remainingGas`?是否大于零,是則返回初始車站的下標;若所有車站都出發都無法走完全程,則返回`-1`。
## 三.示例代碼
~~~
#include <vector>
using namespace std;
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{
int remainingGas = 0;
int resultIndex = 0;
int sum = 0;
for (size_t i = 0; i < gas.size(); ++i) // 對每個車站的情況進行遍歷
{
remainingGas += gas[i] - cost[i];
sum += gas[i] - cost[i];
if (sum < 0)
{
sum = 0;
resultIndex = i + 1;
}
}
if (remainingGas < 0) return -1; // 所有加油站的加油量小于消耗量
else return resultIndex; // 有解
}
};
~~~
幾個處理結果:



## 四.小結
該題是比較有趣的一題,該方法只是多種方法中一種。
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016騰訊軟件開發筆試題)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契數列問題)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波蘭式的計算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和問題)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching