leetcode之Gas Station
来源:互联网 发布:linux elf 加壳 编辑:程序博客网 时间:2024/06/04 19:27
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.
问题链接
cpp代码如下:
class Solution {public: int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { int s=gas.size(); int total=0,sum=0; int ans=0; for(int i=0;i<s;++i){ total+=gas[i]-cost[i]; sum+=gas[i]-cost[i]; if(sum<0){ sum=0; ans=i+1; } } if(total<0)return -1; return ans; }};
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 OJ 之 Gas Station (加油站)
- LeetCode:Gas Station
- [LeetCode] Gas Station
- [leetcode]Gas Station
- Leetcode: Gas Station
- LeetCode:Gas Station
- [LeetCode] Gas Station
- leetcode Gas Station
- 在IDEA中代码自动提示第一个字母大小写必须匹配的解决
- [开源类库/项目] android保存崩溃时的错误信息log至本地【源码+jar包+使用说明
- JMeter压力测试入门教程[图文]
- java中使用Pattern类中和Matcher类进行查找和替换,你会吗?
- C 语言程序设计实践 3.5 Compare
- leetcode之Gas Station
- 浅谈Android里的Context的使用
- 概述排序算法
- Android从零单排之Adapter及其子类
- Python笔记——input( )和raw_input( )
- 那些不能错过的XCode插件
- C 语言程序设计实践 3.6 EAX
- 小马哥-------高仿红米1s 红米 t470主板拆机主板图 电信版 高通芯片
- Oracle 将物理Standby库临时激活用于测试