From c9bff6e42c5f61f1f1ca873131eb2de4aff01003 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Mon, 1 Jan 2024 10:34:33 +0800 Subject: [PATCH] =?UTF-8?q?Update0134.=E5=8A=A0=E6=B2=B9=E7=AB=99=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0134.加油站.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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; + } +} +```