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 ...
随机推荐
- Ring3创建事件Ring0设置事件
应用程序中创建的事件和在内核中创建的事件对象,本质上是同一个东西,在用户模式中,他用句柄表示,在内核模式下,他用KEVENT表示数据结构表示.在应用程序中,所有的内核对象都不会被用户看到,用户看到的知 ...
- IE浏览器强制不是要兼容视图
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta ht ...
- win10家庭版怎么开启Administrator超级管理员帐户
一.win10家庭版开启administrator方法: 1.通过Cortana搜索cmd,匹配出“命令提示符”,右键以管理员身份运行: 2.在打开的命令提示符窗口输入net user admin ...
- 文件上传(asp.net webform中)
1.配置允许上传文件大小 <configuration> <appSettings> <!--配置上传文件最大字节数:单位KB--> <add key=&qu ...
- mybaits插入时的一些总结
我们时长在批量插入时,需要获取插入数据的id. 这样: <insert id="insertUser" parameterType="gys.entity.User ...
- 通过表达式树把datareader和datatable转换为实体
续上两篇文章,使用emit构造dynamic method,把 datareader转换为实体,以避免直接使用反射来实现带来的性能损失.代码看似没有纰漏,但是实际上我在framwork4下运行时,调用 ...
- Java虚拟机------JVM分析工具
主要介绍JVM的分析工具: jps jps:Java Virtual Machine Process Status Tool http://docs.oracle.com/javase/1.5.0/d ...
- angularjs的cache
首先要引入angular-cookies.js插件 angular.module('app').service('cache', ['$cookies', function($cookies){ th ...
- Linux之文档与目录结构 (/ 用法, 相对路径,绝对路径)
Linux之文档与目录结构 Linux文件系统结构 Linux目录结构的组织形式和Windows有很大的不同.首先Linux没有“盘(C盘.D盘.E盘)”的概念.已经建立文件系统的硬盘分区被挂载到 ...
- oracle项目代码
------------------------------------------------ create table DEP_WRTF_RSLT ( sr_no_id ) not null, d ...