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 ...
随机推荐
- 【ZZ】详解哈希表的查找
详解哈希表的查找 https://mp.weixin.qq.com/s/j2j9gS62L-mmOH4p89OTKQ 详解哈希表的查找 2018-03-01 算法与数据结构 来自:静默虚空 http: ...
- UML类关系:依赖、关联、聚合、组合
1,依赖关系(Dependency) 单向,表示一个类依赖于另一个类的定义,其中一个类的变化将影响另外一个类,是一种“use a”关系 如果A依赖于B,则B表现为A的局部变量,方法参数,静态方法调用等 ...
- 00004 - test命令详解
Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值.字符和文件三个方面的测试. -------------------------------------------------- ...
- MySQL 之迁移用户及权限
参考来源: https://www.cnblogs.com/huangmr0811/p/5570994.html https://blog.csdn.net/u011665746/article/de ...
- Linux查看DNS服务器及设置DNS服务器
DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串. 一台主机的dn ...
- javascript-保留2位小数函数方法
function zero(num){ var str=num.toString(); if(str.indexOf(".")==-1){ return num+'.00'; }e ...
- C++ 调用 Lua
直接上代码: 1:c++代码 #include <lua.hpp> #include <LuaBridge/LuaBridge.h> #include <iostream ...
- tomcat简单使用(一)
先来说一说tomcat的使用 官网下载tomcat:tomcat,我的百度云上的:tomcat Tomcat分为安装版和解压版:安装版:一台电脑上只能安装一个Tomcat:解压版:无需安装,解压即可用 ...
- Solr Date类型的哪些你不得不了解的细节
我们先来看看Solr日期类型的一些内幕,然后讨论一下Solr日期类型存在的一些问题,最后我们看看怎么解决现存的问题.概述 DateField 在Solr4.x之前,我们只有DateField,这类型现 ...
- RESTframwork之视图view
一 在view.py 中: class AuthorView(APIView): def get(self, request): author_list = Author.objects.all() ...