We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 6bb97b5 + e0237b5 commit 1a3e805Copy full SHA for 1a3e805
problems/0134.加油站.md
@@ -345,6 +345,37 @@ class Solution:
345
```
346
347
### Go
348
+
349
+贪心算法(方法一)
350
+```go
351
+func canCompleteCircuit(gas []int, cost []int) int {
352
+ curSum := 0
353
+ min := math.MaxInt64
354
+ for i := 0; i < len(gas); i++ {
355
+ rest := gas[i] - cost[i]
356
+ curSum += rest
357
+ if curSum < min {
358
+ min = curSum
359
+ }
360
361
+ if curSum < 0 {
362
+ return -1
363
364
+ if min >= 0 {
365
+ return 0
366
367
+ for i := len(gas) - 1; i > 0; i-- {
368
369
+ min += rest
370
371
+ return i
372
373
374
375
+}
376
+```
377
378
+贪心算法(方法二)
379
```go
380
func canCompleteCircuit(gas []int, cost []int) int {
381
curSum := 0
0 commit comments