【leetcode】Gas Station
Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.
Note:
The solution is guaranteed to be unique.
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int total_oil=;
int index=;
int n=gas.size();
for(int i=;i<n;i++)
{
total_oil+=gas[i];
total_oil-=cost[i];
if(total_oil<)
{
total_oil=;
index=i+;
}
}
//如果循环到了最后一个,没了油,说明任何点都没有希望到达了
if(index==n)
{
return -;
}
//如果一直循环到最后,仍然有油,说明从0点可以遍历整个路径
if(index==)
{
return ;
}
//如果开到最后还剩油,我们继续往后计算。
for(int i=;i<n;i++)
{
total_oil+=gas[i];
total_oil-=cost[i];
//又回到了初始点,则说明从该点可以遍历整个路径
if(i==index)
{
return i;
}
//如果在中间任何一点没有了油,就不必再尝试了
if(total_oil<)
{
break;
}
}
return -;
}
};
【leetcode】Gas Station的更多相关文章
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 【Leetcode】【Medium】Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- 【LeetCode】贪心 greedy(共38题)
[44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
随机推荐
- IIS 8.5 伪静态去掉index.php thinkphp 3.2.2
因为测试都是在win下开发的 win8.1企业版 II8.5 首先安装 Thinkphp 3.2.2 URL Rewrite Module 2.0 http://www.iis.net/downlo ...
- js实现点击增加文本输入框
html代码: <ul id="ulid21" > <li id="li11" >问卷选项设置:</li> </ul& ...
- yii2中事务不能回滚的解决
$player1 = Player::findOne(1); $player1->nickname = '111'; $player2 = Player::findOne(2); $player ...
- 在VS2013中强制IIS Express应用程序池使用经典模式
直接在文件夹地址栏输入 %userprofile%\documents\iisexpress\config\applicationhost.config 会打开上边的配置文件 将 <siteDe ...
- GC垃圾回收机制
学习资料:http://kb.cnblogs.com/page/106720/
- kafka C客户端librdkafka producer源码分析
from:http://www.cnblogs.com/xhcqwl/p/3905412.html kafka C客户端librdkafka producer源码分析 简介 kafka网站上提供了C语 ...
- autopep8
修正python pep8的警告挺无趣的, 用了 autopep8 感觉比较爽. 记录如下. ----------------pep8检查----------------平时我用pydev做pep8检 ...
- angular $resource模块
目录(?)[-] 安装 应用resource 扩展resource 上一篇中讲到使用$http同服务器进行通信,但是功能上比较简单,angularjs还提供了另外一个可选的服务$resource, ...
- WCF--验证码实现...
未开始待续... 未完待续...
- Exception→"Source":"EntityFramework" "Message" :"更新条目时出错。有关详细信息,请参阅内部异常。"
给一个数据库中类型为"datetime"的列赋值为 "DateTime.MinValue"...... 而// ::} But--01到9999-- :: 到 ...