题目

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.

分析

题目描述:有N个加油站点构成一个环状,每个站点i加油量为gas[i],从站点i到站点i+1需要耗费有两为cost[i],现要求从哪个站点出发可以成功转一圈回到初始站点,返回该站点,若没有则返回-1;

详细思路分析参考:算法分析网址

AC代码

  1. class Solution {
  2. public:
  3. int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
  4. if (gas.size() == 0 || cost.size() == 0 || gas.size() != cost.size()) return -1;
  5. int total = 0, sum = 0, start = 0;
  6. for (int i = 0; i < gas.size(); ++i){
  7. total += (gas[i] - cost[i]);
  8. if (sum < 0){ //发现油箱空了,从下一个站点尝试
  9. sum = (gas[i] - cost[i]);
  10. start = i;
  11. }
  12. else
  13. sum += (gas[i] - cost[i]);
  14. }
  15. return total < 0 ? -1 : start; //用total判断start 是否是满足要求的解
  16. }
  17. };

LeetCode(134) Gas Station的更多相关文章

  1. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  2. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  3. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  4. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  5. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  6. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  7. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  8. LeetCode(4)Median of Two Sorted Arrays

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  9. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

随机推荐

  1. 微软官方NET Core 2.0

    NET Core 2.0 微软官方发布的.NET Core 2.0相关的博客: Announcing .NET Standard 2.0 Announcing .NET Core 2.0 F# and ...

  2. ssh登录出现 Host key verification failed. 问题

    我们使用ssh链接linux主机时,可能出现“Hostkey verification failed.“的提示,ssh连接不成功.可能的提示信息如下: @@@@@@@@@@@@@@@@@@@@@@@@ ...

  3. es6新语法:let、const

    关于浏览器的兼容情况,可以访问can i use进行查询. 目前的主要方式还是通过使用Babel编译来解决兼容性问题. 我们目前使用Babel将ES6的代码兼容到了IE8,但这是在放弃某些新特性的条件 ...

  4. scp 可以在 2个 linux 主机间复制文件

    Linux scp命令用于Linux之间复制文件和目录,具体如何使用这里好好介绍一下,从本地复制到远程.从远程复制到本地是两种使用方式.这里有具体举例: ================== Linu ...

  5. 开源分布式Job系统,调度与业务分离-如何创建周期性的HttpJob任务

    项目介绍: Hangfire:是一个开源的job调度系统,支持分布式JOB!! Hangfire.HttpJob 是我针对Hangfire开发的一个组件,该组件和Hangfire本身是独立的.可以独立 ...

  6. pytorch 安装错误,报 GLIBCXX_3.4.20 错误

    pytorch 从源码安装 链接:http://blog.csdn.net/u012442157/article/details/78134888 发现错误: 解决方案: http://blog.cs ...

  7. 一个好用的压力测试工具tsung

    一个好用的压力测试工具tsung          前段时间一直在忙各种事情,快三周没弄过引擎了,今天有点时间,正好之前写的服务器引擎也到了收尾测试的阶段,于是就研究了下怎么测试服务器压力.      ...

  8. sql 容易被忽视的点

    1 dual select查询语句只有select就可以,但为了规范,凑结构,可以加个dual 例:select now() from dual; 这个概念是Oracle中的.在mysql中可写可不写 ...

  9. H5的storage(sessionstorage&localStorage)简单存储删除

    众所周知,H5的storage有sessionstorage&localStorage,其中他们的共同特点是API相同 下面直接上代码,storage中的存储与删除: <!DOCTYPE ...

  10. Python3+Selenium3+webdriver学习笔记10(元素属性、页面源码)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记10(元素属性.页面源码)'''from selenium i ...