diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index 2f9539e8..c093023d 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -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; + } +} +```