在一条环路上有 N 个加油站,其中第 i 个加油站有汽油gas[i]。
你有一辆油箱容量无限的的汽车,从第 i 个加油站前往第 i+1 个加油站需要消耗汽油 cost[i]。你从其中一个加油站出发,开始时油箱为空。
如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回-1。
注意:
给定的数据可保证答案是唯一的。
详见:https://leetcode.com/problems/gas-station/description/

Java实现:

能走完整个环的前提是gas的总量要大于cost的总量,这样才会有起点的存在。假设开始设置起点start = 0, 并从这里出发,如果当前的gas值大于cost值,就可以继续前进,此时到下一个站点,剩余的gas加上当前的gas再减去cost,看是否大于0,若大于0,则继续前进。当到达某一站点时,若这个值小于0了,则说明从起点到这个点中间的任何一个点都不能作为起点,则把起点设为下一个点,继续遍历。当遍历完整个环时,当前保存的起点即为所求。

class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
if(gas==null||cost==null||gas.length!=cost.length){
return -1;
}
int total=0;
int sum=0;
int start=0;
for(int i=0;i<gas.length;++i){
total+=gas[i]-cost[i];
sum+=gas[i]-cost[i];
if(sum<0){
sum=0;
start=i+1;
}
}
return total<0?-1:start;
}
}

参考:https://www.cnblogs.com/grandyang/p/4266812.html

134 Gas Station 加油站的更多相关文章

  1. [leetcode]134. Gas Station加油站

      There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. Y ...

  2. 134. Gas Station加油站

    [抄题]: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...

  3. 134. Gas Station leetcode

    134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...

  4. 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters

    870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...

  5. Leetcode 134 Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  6. [LeetCode] 134. Gas Station 解题思路

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  7. [leetcode greedy]134. Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  8. [LeetCode] Gas Station 加油站问题

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  9. leetcode 134. Gas Station ----- java

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

随机推荐

  1. 解决ubuntu没有/var/log/messages的问题

    1:root身份打开 /etc/rsyslog.d/50-default.conf 2:把注释#去掉 #*.=info;*.=notice;*.=warn;\ # auth,authpriv.none ...

  2. xalion三层与Web开发帖子一览表 good

    使用http.sys,让delphi 的多层服务飞起来(Delphi借用http.sys充当http服务器,也就可以发送返回JSON等信息,当然浏览器也可以使用)http://www.cnblogs. ...

  3. SDUT OJ 1598 周游列国

    周游列国 Time Limit: 1000ms   Memory limit: 32768K  有疑问?点这里^_^ 题目描述 大家都知道孔子吧,春秋战国时候的一个老头儿.当时出国还不用护照,所以他经 ...

  4. xunit输出output到控制台

    1.https://xunit.github.io/docs/capturing-output 里面似乎提到2个方法,第二个方法还需要在配置文件中添加appSetting 这里采用第一种方法, 1.添 ...

  5. DEDE内容页调用栏目的SEO标题、描述、关键字的方法

    上篇写了<dedecms栏目页调用栏目关键词.描述的方法>,本章雨田SEOER讲述DEDE内容页调用栏目的SEO标题.描述.关键字的方法内容页调用SEO标题:在<title>& ...

  6. 使用JavaScript访问XML数据

    在本篇文章中,我们将讲述如何在IE中使用ActiveX功能来访问并解析XML文档,由此允许网络冲浪者操纵它们.这一网页将传入并运行脚本的初始化.你一定确保order.xml文档与jsxml.html在 ...

  7. Silverlight中使用MVVM(1)

    Silverlight中使用MVVM(1)   Silverlight中使用MVVM(1)--基础 Silverlight中使用MVVM(2)—提高 Silverlight中使用MVVM(3)—进阶 ...

  8. position属性中的绝对定位和相对定位

    absolute(绝对定位):1.如果没有父级DIV,则会根据浏览器原始点去定位,而且跟他相邻的DIV会忽略它,定位后则可用TRBL(top,right,bottom,left)去布局.注意:TRBL ...

  9. BZOJ1499 单调队列+DP

    1499: [NOI2005]瑰丽华尔兹 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 1560  Solved: 949[Submit][Status] ...

  10. spring配置mongodb连接副本集多个节点

    mongodb版本3.4.x 1.配置副本集 先配置副本集,可参考我之前写的文章:http://blog.csdn.net/fuck487/article/details/78287362 注意:必须 ...