mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 20:40:39 +08:00
增加0134.加油站Java解法
This commit is contained in:
@ -200,6 +200,7 @@ public:
|
||||
|
||||
Java:
|
||||
```java
|
||||
// 解法1
|
||||
class Solution {
|
||||
public int canCompleteCircuit(int[] gas, int[] cost) {
|
||||
int sum = 0;
|
||||
@ -221,7 +222,26 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
// 解法2
|
||||
class Solution {
|
||||
public int canCompleteCircuit(int[] gas, int[] cost) {
|
||||
int curSum = 0;
|
||||
int totalSum = 0;
|
||||
int index = 0;
|
||||
for (int i = 0; i < gas.length; i++) {
|
||||
curSum += gas[i] - cost[i];
|
||||
totalSum += gas[i] - cost[i];
|
||||
if (curSum < 0) {
|
||||
index = (i + 1) % gas.length ;
|
||||
curSum = 0;
|
||||
}
|
||||
}
|
||||
if (totalSum < 0) return -1;
|
||||
return index;
|
||||
}
|
||||
}
|
||||
```
|
||||
Python:
|
||||
```python
|
||||
class Solution:
|
||||
|
Reference in New Issue
Block a user