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 ...
随机推荐
- HTML-CSS font-family:中文字体的英文名称
本文转自网络,找不到原地址了,在这里保留了作者名. font-family:中文字体的英文名称 ellisontang 发表于2011-07-15 16:33 宋体* SimSun 黑体* SimHe ...
- python3基础操作
ubuntu下python连接mysql apt-get install python-mysqldb 获取当前时间 >>> from datetime import datetim ...
- HibernateUtil工具类的使用
为了简化代码的重复性,使用HibernateUtil工具类对Hibernate有关的代码进行整合 主要实现有,getSessionFactory(),getSession(),closeSession ...
- Spark2.X集群运行模式
rn 启动 先把这三个文件的名字改一下 配置slaves 配置spark-env.sh export JAVA_HOME=/opt/modules/jdk1..0_60 export SCALA_HO ...
- Vue学习记录(一)
一.引入js文件(直接采用CDN): http://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js 二.简单实例: (1)HTML代码: &l ...
- Wsgi的web框架实例
建立server服务端: from wsgiref.simple_server import make_server import time def f1(request): return [b'&l ...
- mysql数据库优化(二)
1.sql防止注入 https://www.cnblogs.com/sevck/p/6733702.html 结果: C:\Users\ASUS\kuaigong3.6.5\lib\site-pack ...
- @Autowired与@Resource 详细诠释和区别(附带例子)
@Autowired 与@Resource:1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配( ...
- WPF 我的初学必备技能
0.控件 0.1.内容控件(Content Controls) 0.2.条目控件(Items Controls) 0.3.文本控件(Text Controls) 0.4.范围控件(Range Cont ...
- 安全测试8_Web安全实战1(DVWA部署)
1.渗透神器的打造(火狐浏览器的打造) Firebug HackBar(渗透插件) Tamper Data(抓包.截包.改包等功能) Proxy Switcher(代理设置) 2.PHP环境安装(ph ...