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

You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.

Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.

Note:
The solution is guaranteed to be unique.

1 如果总的gas - cost小于零的话,那么没有解返回-1

2 如果前面所有的gas - cost加起来小于零,那么前面所有的点都不能作为出发点。

3 选择最佳出发点后,判断从这点出发能否环行一圈。

class Solution {
public: int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
//vector<int> sum;
int sum=;
int res=-;
for(int i=;i<gas.size();i++)
{
int temp=gas[i]-cost[i];
sum+=temp;
if(res==-&&sum>=)
res=i;
else if(sum<)
{
sum=;
res=-;
}
}
if(res!=-)
{
for(int i=;i<res;i++)
{
int temp=gas[i]-cost[i];
sum+=temp;
if(sum<)
{
res=-;
break;
}
} }
return res;
}
};

Gas Station——又是一道经典问题的更多相关文章

  1. [LeetCode] Gas Station,转化为求最大序列的解法,和更简单简单的Jump解法。

    LeetCode上 Gas Station是比较经典的一题,它的魅力在于算法足够优秀的情况下,代码可以简化到非常简洁的程度. 原题如下 Gas Station There are N gas stat ...

  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. [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 ...

  4. [LeetCode 题解]:Gas Station

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 There are ...

  5. [LeetCode] 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] Gas Station 加油站问题

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

  7. PAT 1072. Gas Station (30)

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  8. 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 ...

  9. 【leetcode】Gas Station

    Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...

随机推荐

  1. POI 2018.10.27

    [POI2015]LOG 维护一个长度为n的序列,一开始都是0,支持以下两种操作:1.U k a 将序列中第k个数修改为a.2.Z c s 在这个序列上,每次选出c个正数,并将它们都减去1,询问能否进 ...

  2. [转]Android 如何根据网络地址获取网络图片方法

    http://blog.csdn.net/xiazdong/article/details/7724103 目录(?)[-] h2pre namecode classhtml stylefont-we ...

  3. 用camke编译python程序

    project(test) cmake_minimum_required(VERSION 3.0) find_package(OpenCV REQUIRED) find_package (Python ...

  4. mysql的cast()函数

    先来补个知识: decimal的用法: decimal(18,4)总长18位,包括1位小数点和4为小数,也就是说18-1-4=13整数位只有13位decimal(19,1)总长19位,17位整数,1位 ...

  5. RabbitMQ基础概念(消息、队列、交换机)

    1.消息的确认 RabbitMQ需要对每一条发送的消息进行确认.消费者必须通过AMQP的basic.ack命令显式地向RabbitMQ发送一个确认,或者在订阅到队列的时候就将auto_ack参数设置为 ...

  6. Linux下设置mysql和tomcat开机启动

    本文基于CentOS 64位     一.mysql设置开机启动 1.cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql ...

  7. js高阶函数--判断数据类型、函数胡柯里化;

    一.判断数据类型: 常见的判断有typeof.instanceof. constructor. prototype,先来看typeof: var a = "hello world" ...

  8. 例子Architecting Android…The clean way?----代码分析

    Presention层:   整个应用启动的时候,就执行依赖的初始化.编译项目之后,Dagger依赖框架使用ApplicationComponent生成一个DaggerApplicationCOmpo ...

  9. IntentServicce;Looper;long-running task

    7. If you want to carry on a long-running task, what do you need to do? IntentService:Service Servic ...

  10. bzoj 2083: [Poi2010]Intelligence test——vecto+二分

    Description 霸中智力测试机构的一项工作就是按照一定的规则删除一个序列的数字,得到一个确定的数列.Lyx很渴望成为霸中智力测试机构的主管,但是他在这个工作上做的并不好,俗话说熟能生巧,他打算 ...