leetCode_Gas Station
来源:互联网 发布:世强软件万能五笔 编辑:程序博客网 时间:2024/05/29 15:24
链接:https://leetcode.com/problems/gas-station/
题意:一个环形道路上有n个加油站,每个加油站有油gas[i],加油站之间的道路消耗油cost[i],一开始车的油为空,从哪个点开始走能把一圈走完?
解法:从0点开始算,起点应该为gas[i]-cost[i]之和最小的那个加油站的下一个加油站。代码如下:
class Solution {public: int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { int length=gas.size(),i,c=0,maxV,minV,mIndex=0; if(length==0) return -1; minV=gas[0]-cost[0]; for(i=0;i<length;i++) { c=c+gas[i]-cost[i]; if(c<minV) { minV=c; mIndex=i; } } if(c<0) return -1; else return (mIndex+1)%length; }};
0 0
- Leetcode_gas-station
- Leetcode_Gas Station
- leetcode_Gas Station
- leetCode_Gas Station
- Pets'Station
- shihuo station
- hdu4435charge-station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- 172. Factorial Trailing Zeroes
- solr lucence局部更新
- Binary Tree Upside Down
- PHP 数据库访问问题集锦
- C++带异常处理的CharStack
- leetCode_Gas Station
- Java调用C++动态链接库dll,有详细过程。VS2015+Eclipse以及失败解决方案
- inline内联函数(声明前加inline还是定义前加inline)
- [leetcode] 361. Bomb Enemy 解题报告
- about macros fs_initcall and module_init
- Windows7配置驱动开发环境(使用WDK)
- NOI2014补全+解题报告
- java对象转solrinputdocument
- 第十四周上机实践项目2(2):两个成员的类模板