class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int n = gas.size();
if(n == ) return -; int cnt = ;
int sum = ;
int i = ;
int start = ;
int first_start = -; while(cnt < n)
{
sum += gas[i] - cost[i]; if(sum < )
{
sum = ;
cnt = ;
start = (i+)%n; //从这个i开始的
}
else
{
cnt ++;
} i = (++i)%n; if(start == ) break; }
return cnt == n ? start : -;
}
};

怎么判断无解呢?

循环多少次算无解?

首先,start不保证是连续变化的。所以你记录了第一次的start,后面未必一定经过这个值。可能会跳过去。导致无限循环。比如gas=[2,4],cost=[3,4]的情况。

start = 0,

i = 0 时候, sum = -1 < 0,所以 sum = 0,cnt = 0,start = 1. i++

i = 1时候,sum = 0, ok ,cnt = 1, i = 0.

i = 0时候,sum = -1,又回来了。。。死循环

start 一直等于1.死循环了。检测不到!

leetcode 错误题解,反面教材 Gas Station的更多相关文章

  1. [LeetCode 题解]:Gas Station

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

  2. [Leetcode 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]. ...

  4. LeetCode 134. 加油站(Gas Station)

    题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其 ...

  5. PAT甲题题解-1072. Gas Station (30)-dijkstra最短路

    题意:从m个加油站里面选取1个站点,使得其离住宅的最近距离mindis尽可能地远,并且离所有住宅的距离都在服务范围ds之内.如果有很多相同mindis的加油站,输出距所有住宅平均距离最小的那个.如果平 ...

  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. [LeetCode] Gas Station

    Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...

  8. leetcode@ [134] Gas station (Dynamic Programming)

    https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...

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

随机推荐

  1. 指定分隔符连接数组元素join()

    join()方法用于把数组中的所有元素放入一个字符串.元素是通过指定的分隔符进行分隔的. 语法: arrayObject.join(分隔符) 参数说明: 注意:返回一个字符串,该字符串把数组中的各个元 ...

  2. [UE4]客户端-服务器模式

    客户端负责表现.服务器端负责数据. 以掉血为例: 一.玩家A砍了B一刀 二.服务器计算伤害,修改B的血量 三.把B的血量发给B,A砍B的动作发给所有能看到的玩家 四.客户端播放掉血量(如果允许可见), ...

  3. 阿里直播在线人数只统计rtmp格式的播放源

  4. 知识点:Mysql 基本用法之事务

    事务 事务用于将某些操作的多个SQL作为原子性操作,一旦有某一个出现错误,即可回滚到原来的状态,从而保证数据库数据完整性. 事务实例: create table user( id int primar ...

  5. CentOS之文档的压缩与打包

    .rar压缩文件linux中不识别,.zip在windows和Linux中动能使用. .gz:由gzip压缩工具压缩的文件 .bz2:bzip2压缩工具压缩的文件 .tar:由tar打包程序打包的文件 ...

  6. 第1章 计算机网络和协议(3)_TCP/IP协议

    3. TCP/IP协议 3.1 TCP/IP协议分层 3.2 TCP/IP通信过程 (1)应用层:浏览器和Web服务器是两个对等的实现,它们之间使用http协议进行通信. (2)传输层:网页传输之前, ...

  7. Opening socket connection to server :2181. Will not attempt to authenticate using SASL (unknown error) hbase

    问题: 在HBase机群搭建完成后,通过jdbc连接hbase,在连接zookeeper阶段出现Opening socket connection to server  :2181. Will not ...

  8. Flume数据采集准备

    , flume的官网:http://flume.apache.org/ flume的下载地址:http://flume.apache.org/download.html 这里我们用的是apache版本 ...

  9. react学习笔记(一)

    React的介绍: React来自于Facebook公司的开源项目 React 可以开发单页面应用 spa(单页面应用) react 组件化模块化 开发模式 React通过对DOM的模拟(虚拟dom) ...

  10. ~Vue实现简单答题功能,主要包含单选框和复选框

    内容 实现简单答题效果 环境 Vue,webpack(自行安装) 实现方式 页面将答题列表传递给调用组件,组件将结果返回给调用页面(其它模式也ok,这只是例子) ------------------- ...