博客
关于我
134. Gas Station
阅读量:255 次
发布时间:2019-03-01

本文共 1276 字,大约阅读时间需要 4 分钟。

???????????????????????????????????????????????????????????????????????????????????????????????????

????

  • ????????????????????????????????????????????????-1?
  • ?????????????????????????????????????????????????????????????
  • ???????????????????????????????????????????
  • ????

    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;    }}

    ????

  • ??????????????????????????????????????-1?
  • ?????????????????????????????????
  • ???????????????????????????????????????????????????
  • ???????????????????????????-1?
  • ????????????????????????????????????????????

    转载地址:http://pwxx.baihongyu.com/

    你可能感兴趣的文章
    redis事务操作
    查看>>
    PHP中array_merge和array相加的区别分析
    查看>>
    PHP中dirname(__FILE__)的意思
    查看>>
    PHP中extract()函数的妙用
    查看>>
    PHP中implode()和explode()
    查看>>
    PHP中serialize和json序列化与反序列化的区别
    查看>>
    Redis事务处理
    查看>>
    php中使用ajax进行前后端json数据交互
    查看>>
    Redis事务和锁操作
    查看>>
    PHP中如何得到数组的长度
    查看>>
    php中引入文件几种方式的区别
    查看>>
    PHP中把stdClass Object转array的几个方法
    查看>>
    PHP中替换换行符
    查看>>
    PHP中有关正则表达式的函数集锦
    查看>>
    Redis 集群搭建详细指南
    查看>>
    php中的cookie用法
    查看>>
    php中的session用法
    查看>>
    php中级联,php实现三级级联下拉框_PHP
    查看>>
    PHP中获取星期的几种方法
    查看>>
    Redis 限速器及问题
    查看>>