leetcode 之Gas Station(11)

这题的思路很巧妙,用两个变量,一个变量衡量当前指针是否有效,一个衡量整个数组是否有解,需要好好体会。
int gasStation(vector<int> &gas, vector<int> &cost)
{
int total = ;
int j;
int sum = ;
for (int i = ; i < gas.size(); i++)
{
sum += gas[i] - cost[i];
total += gas[i] - cost[i];
if (sum < )
{
j = i;
sum = ;
}
}
return total> ? j + : -;
}
leetcode 之Gas Station(11)的更多相关文章
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 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 ...
- leetcode@ [134] Gas station (Dynamic Programming)
https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...
- [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 ...
- 【leetcode】Gas Station
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...
- leetcode 134. Gas Station ----- java
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- [LeetCode OJ] Gas Station
问题描述: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...
- LeetCode _ Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- [leetcode]134. Gas Station加油站
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. Y ...
- Java for 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 ...
随机推荐
- POJ3977:Subset——题解(三分+折半搜索)
http://poj.org/problem?id=3977 题目大意:有一堆数,取出一些数,记他们和的绝对值为w,取的个数为n,求在w最小的情况下,n最小,并输出w,n. ————————————— ...
- 添加网站标题logo
如何在标题栏title前添加网站logo? 第一种方法:据说在网站根目录下放着我们的ico型logo,命名为favicon.ico,浏览器会自动去找到并显示.试了试,在firefox23和ie8下都没 ...
- 高效率JavaScript代码的编写技巧
使用DocumentFragment优化多次append 添加多个dom元素时,先将元素append到DocumentFragment中,最后统一将DocumentFragment添加到页面.该做法可 ...
- scanf函数用法小记
By francis_hao Aug 26,2017 scanf – 输入格式转换 概述 #include <stdio.h>int scanf(const char *fo ...
- HDU1045:Fire Net(二分图匹配 / DFS)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- Transformation 线段树好题 好题 (独立写出来对线段树不容易)
Transformation Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 65535/65536 K (Java/Others)T ...
- mapper中的CDATA标签的用法
术语 CDATA 指的是不应由 XML 解析器进行解析的文本数据(Unparsed Character Data). 在 XML 元素中,"<" 和 "&& ...
- stout代码分析之二:None类
stout库中为了避免使用NULL带来的风险,统一用None表示空. None类的实现方式如下: struct None {}; 奇怪的是, Nothing类实现方式与None一模一样..让人怀疑作者 ...
- Semaphore信号量
一.前言 互斥锁 同时只允许一个线程更改数据,而Semaphore是同时允许一定数量的线程更改数据 ,比如厕所有3个坑,那最多只允许3个人上厕所,后面的人只能等里面有人出来了才能再进去. 二.sema ...
- ExtJS下拉列表使用方法(异步传输数据)
最近使用ExtJS下拉列表框(ComboBox)希望完成一个动态下拉列表的功能,即列表中的数据都通过异步方式查询数据库而来,同时在用户选择了列表中的某个值后,可以从后台正确的获取用户选择项所对应的值. ...