Skip to content

Commit 66cd4d1

Browse files
committed
加油站问题文档
1 parent efb2cce commit 66cd4d1

File tree

6 files changed

+12
-5
lines changed

6 files changed

+12
-5
lines changed

LeetCode/Doc/加油站问题.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# 加油站问题(gas-station)
22

33
<center>知识点:贪心</center>
4-
54
## 题目描述
65

76
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
@@ -18,14 +17,17 @@ The solution is guaranteed to be unique.
1817
如果从某个起点出发可以保证你开着你的车绕着赛道走一圈,则返回起点的加油站下标,否则返回-1
1918
返回起点加油站的下标。
2019

21-
注意:给出的实例可以保证解决方案是唯一的。
20+
比如下图:
2221

22+
![image-20190725222358833](http://picture-pool.oss-cn-beijing.aliyuncs.com/2019-07-25-142359.png)
2323

24+
加油站上的数字代表该加油站具有的油量,圆圈上的数字代表该路段需要耗费的油量,那么小车该从哪个加油站出发才能保证顺利走一圈呢?
2425

26+
注意:给出的实例可以保证解决方案是唯一的。
2527

2628
## 解题思路
2729

28-
贪心的思路,计算从每一站出发,到达下一站后车里还能剩余的油,然后按照剩余油的多少进行排序,从剩余油最多的站出发,进行模拟
30+
简单地想,我们当然是想从第1个加油站出发,到达下一个加油站时车里剩余的油尽可能多,这样车里剩下的油还可以继续后面的路程,所以,采取贪心的思路,首先计算从每一站出发,到达下一站后车里还能剩余的油,然后按照剩余油的多少进行排序,从可以剩余油最多的站出发,进行模拟行程,看最后可否走一圈,如果不可以,则换下一个起点
2931

3032

3133
## 代码
@@ -81,4 +83,8 @@ public int canCompleteCircuit(int[] gas, int[] cost) {
8183

8284
```
8385

86+
可以求得上例中的答案为0,即从第0号加油站出发,就像下面这样:
87+
88+
89+
8490
[完整代码](../src/seventeen/Solution.java)
Binary file not shown.
Binary file not shown.
Binary file not shown.

LeetCode/src/seventeen/Solution.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ int Partition(int[][] v, int low, int high) {
9494

9595
public static void main(String[] args) {
9696
Solution solution = new Solution();
97-
int[] gas = {2, 3, 1};
98-
int[] cost = {3, 1, 2};
97+
int[] gas = {3, 2, 5, 3, 4, 3};
98+
int[] cost = {2, 3, 4, 4, 3, 4};
9999
int index = solution.canCompleteCircuit(gas, cost);
100100
System.out.println("startIndex is: " + index);
101101

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
- [**single-number-ii(出现一次的数2)**](./LeetCode/Doc/出现一次的数2.md)
160160
- [candy(糖果问题)](./LeetCode/Doc/糖果问题.md)
161161
- [Gas-station(加油站问题)](./LeetCode/Doc/加油站问题.md)
162+
- [clone-graph(图的复制)](./LeetCode/Doc/图的复制.md)
162163

163164

164165

0 commit comments

Comments
 (0)