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. 数组连接a.concat(b),b作为一个整体

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  2. 学习笔记之Everything

    Everything (software) - Wikipedia https://en.wikipedia.org/wiki/Everything_(software) Everything is ...

  3. idea 配置全局 maven

    Idea默认的全局设置,如Maven等 每次使用IDEA打开一个新的项目,maven都需要重新配置,非常不开心,所以需要有个默认全局配置,打破不开心 配置完就OK了 原文链接:https://www. ...

  4. [UE4]Switch on String,根据字符串决定条件分支,类似于高级语言中的switch语句

  5. [UE4]射击产生弹孔:Spawn Decal At Location、Spawn Decal Attached

    Spawn Decal At Location.Spawn Decal Attached 在指定的位置生成一个材质贴上去 产生随意旋转角度

  6. virtualbox创建虚拟机

    两种方式: 1.使用.vdi硬盘镜像文件 2.使用操作系统新建 方法1: #结束! 方法2:

  7. Django跨域问题

    相关博客地址 同源策略与Jsonp 同源策略 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响.可 ...

  8. 避免crontab输出日志

    在cron的自动执行语句后加上> /dev/null 2>&1 例:4 3 * * * /usr/bin/a.sh > /dev/null 2>&1这样就OK拉 ...

  9. 503 Error: need EHLO and AUTH first

    设置OUTLOOK2013使用QQ邮箱,按照QQ邮箱的配置介绍设置好后,收邮件的服务可以了,但是发送邮件的服务失败,报错:503 Error: need EHLO and AUTH first,经查, ...

  10. Navicat Premium 12.0.18 / 12.0.24安装与激活

    若使用Navicat Premium 12.1.8.0请转至Navicat Premium 12.1.8.0安装与激活,其实每个小版本更迭变化不大.另外最重要的是,请仔细阅读本文激活部分,总有一些人遇 ...