[leetcode] Gas Station
来源:互联网 发布:mac音量键在哪里设置 编辑:程序博客网 时间:2024/06/06 17:32
Gas Station
思路:设置两个变量,sum判断当前的gas station的有效性;total则判断整个vector是否有解,有就返回通过sum得到的下标+1,没有则返回-1。
class Solution {public: int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { int total=0; int sum=0; int res=-1; for(int i=0;i<gas.size();++i){ sum+=gas[i]-cost[i]; total+=gas[i]-cost[i]; if(sum<0){ res=i;//不能满足需求的最后一个点 sum=0; } } if(total>=0){ return res+1;//不能满足需求的最后一个gas station的下一个gas station即可满足需求 }else{ return -1; } }};
0 0
- 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
- mac os x 如何打开多个终端
- 大端和小端及如何判断
- [leetcode] Candy
- 硬盘中出现eula.1028.txt等垃圾文件的原因及是否可删
- poj 1657 Distance on Chessboard
- [leetcode] Gas Station
- 坐标转换
- Python如何调用C语言接口
- sqlite学习笔记9:C语言中使用sqlite之插入数据
- android 细节之android.view.InflateException: Binary XML file line #95: Error inflating class(out of m)
- 【LeetCode】Remove Duplicates from Sorted List & Remove Duplicates from Sorted List II
- 循环链表测试自编
- POJ-2472 106 miles to Chicago
- cocos2d-x类型转换(CCstring int string char UTF-8互转)