mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Update0134.加油站,添加C#
This commit is contained in:
@ -630,6 +630,29 @@ object Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```csharp
|
||||||
|
// 贪心算法,方法二
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public int CanCompleteCircuit(int[] gas, int[] cost)
|
||||||
|
{
|
||||||
|
int curSum = 0, totalSum = 0, start = 0;
|
||||||
|
for (int i = 0; i < gas.Length; i++)
|
||||||
|
{
|
||||||
|
curSum += gas[i] - cost[i];
|
||||||
|
totalSum += gas[i] - cost[i];
|
||||||
|
if (curSum < 0)
|
||||||
|
{
|
||||||
|
start = i + 1;
|
||||||
|
curSum = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (totalSum < 0) return -1;
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
Reference in New Issue
Block a user