Gas Station
来源:互联网 发布:odn无源分配网络包括 编辑:程序博客网 时间:2024/05/29 12:10
Q:
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.
Solution:
Reference link: http://blog.csdn.net/kenden23/article/details/14106137
public class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { int sum = 0; int j = -1; int total = 0; for (int i = 0; i < gas.length; i++) { sum = sum + gas[i] - cost[i]; total = total + gas[i] - cost[i]; if (sum < 0) { sum = 0; j = i; } } return total < 0? -1: j+1; }}
0 0
- 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
- Objective-C的setter和getter
- td内容 换行和不换行
- Android 屏幕截图并且发送email
- 着诅资住诅棕酌椎浊注桌座茁族宗钻转庄
- 滚动控制小记(笔记)
- Gas Station
- 启动Tomcat时一闪而过
- 安卓TV开发(六) 移动智能终端UI之实现类似GridView的焦点控制FocusView框架
- 软件工程师面试7大技巧
- 酌祖滓缀住滓籽棕兹壮嘴转座租拽赘追爪
- 解析json数据
- 业内人说:小米、黄太吉、罗辑思维的互联网思维假象
- springMVC 下访问html页面
- 邹撞作租酌祝注状缀紫状阻妆拽佐卒琢卒