LeetCode
  • Introduction
  • 第一章: 基本结构
    • 1.1 数组
      • Q11. Container With Most Water
      • Q16. 3Sum Closest
      • Q118. Pascal's Triangle
      • Q119. Pascal's Triangle II
      • Q120. Triangle
      • Q134. Gas Station
    • 1.2 链表
      • Q2: Add Two Numbers
      • Q19. Remove Nth Node From End of List
      • Q82. Remove Duplicates from Sorted List II
      • Q86: Partition List
      • Q92. Reverse Linked List II
      • Q141. Linked List Cycle
      • Q142. Linked List Cycle II
      • Q147. Insertion Sort List
      • Q160. Intersection of Two Linked Lists
      • Q206. Reverse Linked List
    • 1.3 哈希
      • Q1: Two Sum
      • Q3. Longest Substring Without Repeating Characters
    • 1.4 堆栈
      • Q84: Largest Rectangle in Histogram
      • Q155. Min Stack
      • Q20. Valid Parentheses
      • Q225. Implement Stack using Queues
      • Q232. Implement Queue using Stacks
    • 1.5 树
      • Q94. Binary Tree Inorder Traversal
      • Q100. Same Tree
      • Q101. Symmetric Tree
      • Q102. Binary Tree Level Order Traversal
      • Q103. Binary Tree Zigzag Level Order Traversal
      • Q104. Maximum Depth of Binary Tree
      • Q105. Construct Binary Tree from Preorder and Inorder Traversal
      • Q106. Construct Binary Tree from Inorder and Postorder Traversal
      • Q107. Binary Tree Level Order Traversal II
      • Q108. Convert Sorted Array to Binary Search Tree
      • Q109. Convert Sorted List to Binary Search Tree
      • Q110. Balanced Binary Tree
      • Q111. Minimum Depth of Binary Tree
      • Q112. Path Sum
      • Q113. Path Sum II
      • Q114. Flatten Binary Tree to Linked List
      • Q116. Populating Next Right Pointers in Each Node
      • Q117. Populating Next Right Pointers in Each Node II
      • Q129. Sum Root to Leaf Numbers
      • Q144. Binary Tree Preorder Traversal
    • 1.6 图
    • 1.7 二进制
      • Q89. Gray Code
      • Q136. Single Number
      • Q137. Single Number II
      • Q191. Number of 1 Bits
      • Q190. Reverse Bits
    • 1.8 字符串
      • Q5. Longest Palindromic Substring
      • Q14. Longest Common Prefix
      • Q125. Valid Palindrome
  • 第二章: 动态规划
    • Q85: Maximal Rectangle
    • Q91. Decode Ways
    • Q121. Best Time to Buy and Sell Stock
    • Q198. House Robber
  • 第三章: 递归
    • Q17. Letter Combinations of a Phone Number
    • Q78. Subsets
    • Q86. Scramble String
    • Q90: Subsets II
  • 第四章:贪心
    • Q122. Best Time to Buy and Sell Stock II
  • 第五章:分治法
  • 第六章:数学
    • Q6. ZigZag Conversion
    • Q7. Reverse Integer
    • Q9. Palindrome Number
    • Q168. Excel Sheet Column Title
    • Q171. Excel Sheet Column Number
  • 第七章:查找
    • Q15. Three Sum
    • Q167. Two Sum II
    • Q169. Majority Element
  • 第八章:排序
Powered by GitBook
On this page
  • 分析
  • C++代码

Was this helpful?

  1. 第一章: 基本结构
  2. 1.1 数组

Q134. Gas Station

PreviousQ120. TriangleNext1.2 链表

Last updated 5 years ago

Was this helpful?

直达:

There areNgas stations along a circular route, where the amount of gas at stationiisgas[i].

You have a car with an unlimited gas tank and it costscost[i]of gas to travel from stationito 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.

分析

难点是理清思路,分析出该问题存在的几个性质。

性质1:如果gas[i] < cost[i],则i不能作为起点;

性质2:对所有路径求和,如果∑(gas[i]−cost[i])>0\sum{(gas[i] - cost[i])} > 0∑(gas[i]−cost[i])>0,则一定存在一点可以遍历整个环。

问题是当确定能够便利之后,如何确定起点:

性质3:如果i能够作为起点,则从i出发可以到任何一个点;

性质4:如果i是唯一的一个点,选择不存在点j可以到i;

性质5:如果i是唯一的一个点,则i一定可以到i+1到n,0到i-1一定不能到i

根据上面三个性质,得到解决问题的思路:

  1. 根据性质2,遍历两个数组,确定能不能完成环的遍历;能的话,进入2,否则直接返回-1;

  2. 根据性质5,从0出发,查找0不能到的i,如果i+1能到n,则可以返回i+1, 如果i+1不能到n,则将i更新为i能到的最近的节点。

C++代码

class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int sum = 0;
        int cur_start = 0;
        int res = 0;
        for (int i=0; i<gas.size(); i++){
            sum += (gas[i] - cost[i]);
            cur_start += (gas[i] - cost[i]);
            if (cur_start < 0){
                cur_start = 0; //重新开始计数
                res = i+1;
            }
        }
        if (sum < 0) return -1;
        else{
            if(res >= gas.size()) return 0;
            else return res;
        }
    }
};
https://leetcode.com/problems/gas-station/description/