【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-十大经典排序算法
随机推荐
- DNS域传送漏洞利用
DNS区域传送(DNS zone transfer)指的是一台备用服务器使用来自主服务器的数据刷新自己的域(zone)数据库.这为运行中的DNS服务提供了一定的冗余度,其目的是为了防止主的域名服务器因 ...
- android控件库(1)-带删除功能的EditText
DJEditText.java /** * Created by xp.chen on 2016/11/25. */ public class DJEditText extends AppCompat ...
- Java byte位移操作 注意事项
Java对byte 的 + - * / >> >>> << & | ^ (加,减,乘,除,右移,左移,无符号右移,位与,位或,位异或)操作,均会是首先 ...
- 基本linux命令
1.mkdir mkdir 创建目录 mkdir -p 循环创建目录 2.cd 切换目录 3.pwd 查看当前路径 4.mkdir 删除一个空的目录 5.cp 复制文件/目录 -r用 ...
- $(document).ready(){}、$(fucntion(){})、(function(){})(jQuery)onload()的区别
1.首先说JQuery的几个写法 $(function(){ //do someting }); $(document).ready(function(){ //do so ...
- java之google style
Google的Java编码规范英文版: http://google-styleguide.googlecode.com/svn/trunk/javaguide.html Google的Java编码规范 ...
- Java转json
一.json介绍 1. 作用:JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,是存储和交换文本信息的语法. 2.json以key-value的格式书写,数 ...
- thinkphp中的自动验证
array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间]) 1.验证字段 需要验证的表单字段名称,这个字段不一定是数据库字段,也可以是表单的一些辅助字段,例如确认密码和验证码等等.有 ...
- static 的使用
static用法小结 转自 http://blog.csdn.net/Kendiv/article/details/675941 在C语言中,static的字面意思很容易把我们导入歧途,其实它的作用有 ...
- Try-Catch机制使用场景分析
(一)在什么场景下加Try-Catch机制 1)以业务逻辑功能为单位,在最上层加Try-Catch机制.为什么要这样做呢?这主要是增加程序的健壮性,防止因抛出异常过多,导致程序崩溃. try { ...