leetcode17gas-station
题目描述
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.
输入
[2,3,1],[3,1,2]
输出
1
class Solution {
public:
/**
*
* @param gas int整型vector
* @param cost int整型vector
* @return int整型
*/
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
// write code here
int start=gas.size()-1;
int end=0;
int sum=gas[start]-cost[start];
while (start>end)
{
if (sum>=0){
sum+=gas[end]-cost[end];
++end;
}else {
--start;
sum+=gas[start]-cost[start];
}
}
return sum>=0 ?start:-1;
}
};
leetcode17gas-station的更多相关文章
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- PAT 1072. Gas Station (30)
A gas station has to be built at such a location that the minimum distance between the station and a ...
- Leetcode 134 Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- POJ 2031 Building a Space Station
3维空间中的最小生成树....好久没碰关于图的东西了..... Building a Space Station Time Limit: 1000MS Memory Li ...
- 【leetcode】Gas Station
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...
- [LeetCode] Gas Station
Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...
- 20. Candy && Gas Station
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- [ACM_搜索] POJ 1096 Space Station Shielding (搜索 + 洪泛算法Flood_Fill)
Description Roger Wilco is in charge of the design of a low orbiting space station for the planet Ma ...
- Service Station - An Introduction To RESTful Services With WCF
Learning about REST An Abstract Example Why Should You Care about REST? WCF and REST WebGetAttribute ...
- LeetCode——Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
随机推荐
- Azure内容审查器之羞羞图审查
上一篇 Azure 内容审查器之文本审查我们已经介绍了如果使用Azure进行文字内容的审核.对于社区内容,上传的图片是否含有羞羞内容也是需要过虑的.但是最为一般开发者自己很难实现这种级别的智能识别.但 ...
- JVM笔记五-堆区
JVM笔记五-堆区 在JVM中,堆区是重中之重.通过前面文章的学习,我们知道了,栈区是不会有垃圾回收的,所以,经常说的垃圾回收,其实就是回收的是堆区的数据.在这里,我们将会看到传说中的,新生代.老年代 ...
- 异步编程新方式async/await
一.前言 实际上对async/await并不是很陌生,早在阮大大的ES6教程里面就接触到了,但是一直处于理解并不熟练使用的状态,于是决定重新学习并且总结一下,写了这篇博文.如果文中有错误的地方还请各位 ...
- python数据清洗
盖帽法 分箱法 简单随机抽和分层抽
- 实时离线一体化在资产租赁saas服务中使用
流水查询需求 需求第一期: 基于TB级的在线数据,支持缴费帐单明细在线查询.大家都知道,像银行帐单流水一样,查几年的流水是常有的事. 支持的维度查询:帐期.欠费状态.日期范围.费用科目类型.房屋分类. ...
- git merge 与 git rebase的区别?
一,git merge 与 git rebase的区别 1,git merge 例如: master分支合并dev分支,git将两个分支dev和master上的所有commit , 按照提交时间的先后 ...
- CentOS下编译搭建LAMP环境
搭建LAMP环境须知 搭建LAMP环境时,需要安装的所有软件都要按照一定的顺序安装,我们按照Apache->MySQL->PHP的顺序安装.但是在安装PHP之前,应先安装PHP5需要的最新 ...
- Java Map转成xml标签字符串
一个简单的java实现,供参考: package com.trilogy.session.data; import java.lang.reflect.Field; import java.util. ...
- Linux用户和组的配置文件
用户和组的主要配置文件 前两个是放用户账号相关的,后两个是放和组相关的 /etc/passwd:用户及其属性信息(名称.UID.主组ID等) #早期密码也放这里,后来发现不安全,谁都能看 /etc/s ...
- Linux配置阿里epl源
去阿里云 有源仓库 阿里云镜像官方站点 https://developer.aliyun.com/mirror/ 先备份本机上的源 mv /etc/yum.repos.d/CentOS-Base.re ...