本文共 1276 字,大约阅读时间需要 4 分钟。
???????????????????????????????????????????????????????????????????????????????????????????????????
import java.util.Arrays;public class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { int n = gas.length; int sumGas = Arrays.stream(gas).sum(); int sumCost = Arrays.stream(cost).sum(); if (sumGas < sumCost) { return -1; } for (int i = 0; i < n; i++) { int current = gas[i]; boolean feasible = true; for (int j = 0; j < n; j++) { int next = (i + j) % n; if (current < cost[next]) { feasible = false; break; } current -= cost[next]; current += gas[next]; } if (feasible) { return i; } } return -1; }} ????????????????????????????????????????????
转载地址:http://pwxx.baihongyu.com/