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. Jmeter(一)简介以及环境搭建

    刚刚在打扫卫生的时候,就一直在思考近一年以来所学知识及体系.知识太过于碎片化,整理的东西全写在笔记本上,日常工作不可能全部用到,所以复习很重要.因此开始准备将一些知识写在随笔里边,用于知识体系的重建, ...

  2. join、on、where、having的使用区别

    on.where.having的区别 on.where.having这三个都可以加条件的子句中,on是最先执行,where次之,having最后.on是在生成中间的临时表时起作用的,where,hav ...

  3. python中编码问题

    各种编码在内存中所占的大小: ascii: 英文:8bit (1B) uft-: 英文:8bit (1B) 中文:24bit (3B) GBK: 英文:8bit (1B) 中文:16bit (2B) ...

  4. 2-java内省机制(Introspector)

    来一个简单的示例吧 package com.my.test; import java.beans.BeanInfo; import java.beans.Introspector; import ja ...

  5. U3d学习001-RollBox例子

    1.世界坐标系和局部坐标系(参照物坐标系)——以参照物为父物体节点  2.刚体组件:       获得GetComponent<Rigidbody>();     移动AddForce(n ...

  6. cocos源码分析--用Sprite加载自定义着色器

    本文写一个使用动态更新属性变量的自定义着色器.在这个例子中,小图标的位置根据手指的触摸而移动,以屏幕重点为参照物,屏幕中向下的部分根据手指的点击乘以一个绿色的颜色值,向上的部分乘以一个红色的颜色值. ...

  7. PHP:引用PhpExcel导出数据到excel表格

    我使用的是tp3.2框架(下载地址:http://www.thinkphp.cn/topic/38123.html) 1.首先要下载PhpExcel类库,放在如下图目录下 2.调用方法 public ...

  8. MySQL高可用架构之基于MHA的搭建

    一.MySQL MHA架构介绍: MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Fa ...

  9. jQuery实现鼠标滑过图片列表加遮罩层

    这个例子实现的功能是:有一列图片列表,鼠标滑过时,将有遮罩层的另一张图盖在该图片的上方,实现鼠标hover的效果. 一.HTML代码: <div class="home-content ...

  10. hive中sql使用英文分号

    hql只要遇见分号则认识是语句的EOF,所以对于分号,需要用“\“转义. 例如: insert overwrite table test_json_map select '{"account ...