Leetcode: Gas Station
来源:互联网 发布:下载淘宝全套免费模板 编辑:程序博客网 时间:2024/05/21 03:24
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.
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { // Note: The Solution object is instantiated only once.int total = 0;int currentgas = 0; int startpoint = -1;int sz = gas.size();for(int i = 0; i < sz; i++){currentgas += gas[i] - cost[i];total += gas[i] - cost[i];if(currentgas < 0){startpoint = i;currentgas = 0;}}return total >= 0 ? startpoint+1 : -1; }
- LeetCode:Gas Station
- [LeetCode] Gas Station
- [leetcode]Gas Station
- Leetcode: Gas Station
- LeetCode:Gas Station
- [LeetCode] Gas Station
- leetcode Gas Station
- [LeetCode]Gas Station
- LeetCode 之 Gas Station
- leetcode Gas Station
- 【Leetcode】Gas Station
- LeetCode | Gas Station
- LeetCode之Gas Station
- Leetcode: Gas Station
- LeetCode OJ:Gas Station
- [LeetCode]134.Gas Station
- [Leetcode] Gas Station (Java)
- gas station leetcode
- 结构体对其问题
- Asp.net Basic Part(1)
- linux- Shell变量命令之单引号与双引号的区别
- fsockopen学习笔记
- 一个正整数去掉s位后得到最小整数
- Leetcode: Gas Station
- POJ 2104 K-th Number
- 在windows下双击可执行jar文件不可以执行的问题
- Android开发中Activity与Service之间getSharedPreferences不同步的解决方法
- OCP-1Z0-053-V12.02-529题
- CompanyOfHeroes2渲染技术
- 面向对象和生活的切合点。
- paip.python 调用qt ui 总结
- linux下auto工具制作Makefile源码包