Gas Station
来源:互联网 发布:c语言大小写敏感吗 编辑:程序博客网 时间:2024/05/04 00:25
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) { // Note: The Solution object is instantiated only once and is reused by each test case. int total = 0,sum = 0,diff = 0;; int j = -1; for(int i = 0;i<gas.size();i++) { diff = gas[i]-cost[i]; total += diff; sum += diff; if(sum < 0) { j = i; sum = 0; } } return (total >=0)?j+1:-1; }};
40 ms
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- 贪吃蛇游戏代码
- [译]Kinect for Windows SDK开发入门(十四):进阶指引 上
- OCP-1Z0-051-V9.02-56题
- android jni socket
- js 判断浏览器类型
- Gas Station
- rails字符串转换表的实体对象
- [译]Kinect for Windows SDK开发入门(十五):进阶指引 下
- 百思为科面试题
- oracle表分区详解
- 存活率问题
- 算法习题17:在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b
- 编写安全代码——不要用memcmp比较structure
- Cocos2d-x 2.0 序列帧动画 深入分析