leetcode 错误题解,反面教材 Gas Station
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的更多相关文章
- [LeetCode 题解]:Gas Station
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 There are ...
- [Leetcode 134]汽车加油站 Gas Station (环形)
[题目] There are N gas stations along a circular route, where the amount of gas at station i is gas[i] ...
- LeetCode(134) Gas Station
题目 There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...
- LeetCode 134. 加油站(Gas Station)
题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其 ...
- PAT甲题题解-1072. Gas Station (30)-dijkstra最短路
题意:从m个加油站里面选取1个站点,使得其离住宅的最近距离mindis尽可能地远,并且离所有住宅的距离都在服务范围ds之内.如果有很多相同mindis的加油站,输出距所有住宅平均距离最小的那个.如果平 ...
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- [LeetCode] Gas Station
Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...
- leetcode@ [134] Gas station (Dynamic Programming)
https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...
- [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 ...
随机推荐
- 信息安全-加密:DES 加密
ylbtech-信息安全-加密:DES 加密 DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法,1977年被美国联邦政府的国家标准局确定为联邦资 ...
- for循环计算li的个数
今天有一段代码 在ie6下面显示 我检查了一下代码,发现每for循环一次,就会重新计算li的个数,会拖慢运行速度,所以改成以下代码,ie6就正常了
- 搭建好lamp,部署owncloud。
先上传两个文件 上传到root目录 上传到opt目录下 #mkdir /opt/dvd1 #mount /dev/sr0 /opt/dvd1 #cd /etc/yum.repos.d/ #vi dvd ...
- echarts折现图配置
js引用和div容器 <div id="container" style="height: 100%"></div> <scrip ...
- Processing Binary Protocols with Client-Side JavaScript
http://blog.mgechev.com/2015/02/06/parsing-binary-protocol-data-javascript-typedarrays-blobs/ https: ...
- HBase 集群部署
前提条件:hadoop及zookeeper机群已经搭建好. 配置hbase集群步骤: 1.配置hbase集群,要修改3个文件 注意:要把hadoop的hdfs-site.xml和core-site. ...
- 集群中使用chronyc同步时间
在集群之中,有些服务器之间的时间需要同步,但并不是所有机器可以直接连外网,这时可以用Chrony工具解决. 解决方法是将其中一台设为时间服务器,然后其它服务器和这台时间服务器同步即可.具体步骤如下: ...
- 在windows server 2012/2016上,任务管理器性能页面增加磁盘监控的办法
从windows server 2012开始,微软修改了任务管理器的显示方式,图像化看起来更直观了,但是可惜的是,默认情况下,2012和2016均只显示CPU/内存/网络三个资源监视,没有重要的磁盘, ...
- js 数组的crud操作
增加push(); 向数组尾添加元素unshift(); 向数组头添加元素向数组指定下标添加元素:可以用Array提供的splice(); var arr = ['a','b','c']; arr.s ...
- day3----编码-集合-深浅copy-文件操作-函数初识
本文档主要内容: 一 编码 二 集合 三 深浅copy 四 文件操作 五 函数初识 首先,我们来看看两个字符串的比较 打开cmd,进入dos界面 >>>a='xingchen@' & ...