From d327806115b66777e8e19ee27af10d4491b7a609 Mon Sep 17 00:00:00 2001 From: ArthurP Date: Sun, 29 Aug 2021 10:37:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200134.=E5=8A=A0=E6=B2=B9?= =?UTF-8?q?=E7=AB=99.md=20C=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0134.加油站.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index ff54fbc6..ff3bc8b2 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -289,21 +289,26 @@ int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){ int curSum = 0; int i; int min = INT_MAX; + //遍历整个数组。计算出每站的用油差。并将其与最小累加量比较 for(i = 0; i < gasSize; i++) { int diff = gas[i] - cost[i]; curSum += diff; - if(min > curSum) + if(curSum < min) min = curSum; } + //若汽油总数为负数,代表无法跑完一环。返回-1 if(curSum < 0) return -1; + //若min大于等于0,说明每一天加油量比用油量多。因此从0出发即可 if(min >= 0) return 0; + //若累加最小值为负,则找到一个非零元素(加油量大于出油量)出发。返回坐标 for(i = gasSize - 1; i >= 0; i--) { min+=(gas[i]-cost[i]); if(min >= 0) return i; } + //逻辑上不会返回这个0 return 0; } ```