Update0134.加油站,添加C#

This commit is contained in:
eeee0717
2024-01-01 10:34:33 +08:00
parent 85f8efeef6
commit c9bff6e42c

View File

@ -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">