Skip to content

Commit b75de55

Browse files
authored
Merge pull request #292 from HCH1212/master
add no.134 answer
2 parents 25c03cf + 281ae8b commit b75de55

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

leetcode/0134.Gas-Station/Gas.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package leetcode
2+
3+
func canCompleteCircuit(gas []int, cost []int) int {
4+
totalGas := 0
5+
totalCost := 0
6+
currGas := 0
7+
start := 0
8+
9+
for i := 0; i < len(gas); i++ {
10+
totalGas += gas[i]
11+
totalCost += cost[i]
12+
currGas += gas[i] - cost[i]
13+
14+
if currGas < 0 {
15+
start = i + 1
16+
currGas = 0
17+
}
18+
}
19+
20+
if totalGas < totalCost {
21+
return -1
22+
}
23+
24+
return start
25+
26+
}

leetcode/0134.Gas-Station/Gas_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question134 struct {
9+
para134
10+
ans134
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para134 struct {
16+
one []int
17+
two []int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans134 struct {
23+
one int
24+
}
25+
26+
func Test_Problem134(t *testing.T) {
27+
qs := []question134{
28+
29+
{
30+
para134{[]int{1, 2, 3, 4, 5}, []int{3, 4, 5, 1, 2}},
31+
ans134{3},
32+
},
33+
34+
{
35+
para134{[]int{2, 3, 4}, []int{3, 4, 3}},
36+
ans134{-1},
37+
},
38+
}
39+
40+
fmt.Printf("------------------------Leetcode Problem 134------------------------\n")
41+
42+
for _, q := range qs {
43+
_, p := q.ans134, q.para134
44+
fmt.Printf("【input】:%v, %v 【output】:%v\n", p.one, p.two, canCompleteCircuit(p.one, p.two))
45+
}
46+
fmt.Printf("\n\n\n")
47+
}

leetcode/0134.Gas-Station/README.md

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# [134. Gas Stations](https://leetcode.com/problems/gas-station/)
2+
3+
4+
## 题目
5+
6+
You are given a list of gas stations on a circular route, where the amount of gas at station i is gas[i], and the cost to travel from station i to its next station (i+1) is cost[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.
7+
8+
Write a function that returns the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1. If there exists a solution, it is guaranteed to be unique.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2]
14+
Output: 3
15+
Explanation: Start at station 3 (index 3), you have 4 liters of gas. Now you have a total of =0+4=4 liters of gas.
16+
Travel to station 4. Now you have 4 - 1 + 5 = 8 liters of gas.
17+
Travel to station 0. Now you have 8 - 2 + 1 = 7 liters of gas.
18+
Travel to station 1. Now you have 7 - 3 + 2 = 6 liters of gas.
19+
Travel to station 2. Now you have 6 - 4 + 3 = 5 liters of gas.
20+
Travel to station 3 again. This time you will have consumed 5 liters of gas, which is exactly enough for you to return to station 3.
21+
Therefore, 3 can be the starting index.
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: gas = [2,3,4], cost = [3,4,3]
28+
Output: -1
29+
Explanation: You cannot start at station 0 or 1 because there is not enough gas to get you to the next station.
30+
We start at station 2, we get 4 liters of gas. Now you have a total of =0+4=4 liters of gas.
31+
Travel to station 0. Now you have 4 - 3 + 2 = 3 liters of gas.
32+
Travel to station 1. Now you have 3 - 3 + 3 = 3 liters of gas.
33+
You cannot return to station 2 because it requires 4 liters of gas but you only have 3 liters in your tank.
34+
Therefore, no matter what, you cannot travel around the ring.
35+
```
36+
37+
**Constraints:**
38+
39+
- `gas.length == n`
40+
- `cost.length == n`
41+
- `1 <= n <= 10^5`
42+
- `0 <= gas[i], cost[i] <= 10^4`
43+
44+
## 题目大意
45+
46+
在一条环路上有 n 个加油站,其中第 i 个加油站有汽油 gas[i] 升。
47+
48+
你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。
49+
50+
给定两个整数数组 gas 和 cost ,如果你可以按顺序绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1 。如果存在解,则 保证 它是 唯一 的。
51+
52+
## 解题思路
53+
54+
- 初始化总油量(totalGas)和总消耗(totalCost)为0,起始点(start)为0,当前油量(currGas)为0。
55+
遍历每个加油站,累加油量和消耗,计算当前油量。
56+
如果当前油量小于0,说明从上一个加油站到当前加油站的油量不足以到达下一个加油站,因此将起始点设置为当前加油站的下一个位置,并将当前油量重置为0。
57+
遍历结束后,如果总油量小于总消耗,说明无法完成整个旅程,返回-1;否则返回起始点。
58+
59+
## 代码
60+
61+
```go
62+
package leetcode
63+
64+
func canCompleteCircuit(gas []int, cost []int) int {
65+
totalGas := 0
66+
totalCost := 0
67+
currGas := 0
68+
start := 0
69+
70+
for i := 0; i < len(gas); i++ {
71+
totalGas += gas[i]
72+
totalCost += cost[i]
73+
currGas += gas[i] - cost[i]
74+
75+
if currGas < 0 {
76+
start = i + 1
77+
currGas = 0
78+
}
79+
}
80+
81+
if totalGas < totalCost {
82+
return -1
83+
}
84+
85+
return start
86+
}
87+
88+
```

0 commit comments

Comments
 (0)