这题的思路很巧妙,用两个变量,一个变量衡量当前指针是否有效,一个衡量整个数组是否有解,需要好好体会。

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)的更多相关文章

  1. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  2. 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 ...

  3. leetcode@ [134] Gas station (Dynamic Programming)

    https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...

  4. [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 ...

  5. 【leetcode】Gas Station

    Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...

  6. 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 ...

  7. [LeetCode OJ] Gas Station

    问题描述: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...

  8. LeetCode _ Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  9. [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 ...

  10. 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 ...

随机推荐

  1. Android 数据库存储之db4o

    在Android中,使用数据库除了可以使用Android内嵌的SQLite,还可以使用db4odb4o是嵌入式的面向对象的数据库,是基于对象的数据库,操作的数据本身就是对象.特点:对象以其本身的方式来 ...

  2. POJ3254:Corn Fields——题解

    http://poj.org/problem?id=3254 题面来自洛谷:https://www.luogu.org/problemnew/show/1879 农场主John新买了一块长方形的新牧场 ...

  3. BZOJ5314:[JSOI2018]潜入行动——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=5314 https://www.luogu.org/problemnew/show/P4516 ht ...

  4. 洛谷 P3959 宝藏 解题报告

    P3959 宝藏 题目描述 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 \(n\) 个深埋在地下的宝藏屋, 也给出了这 \(n\) 个宝藏屋之间可供开发的 \(m\) 条道路和它们的长度. 小 ...

  5. PHP检测json格式数据

    首先要记住json_encode返回的是字符串, 而json_decode返回的是对象 判断数据不是JSON格式: 复制代码代码如下: function is_not_json($str){      ...

  6. phalcon安装

    参考网站:https://docs.phalconphp.com/zh/latest/reference/tools.html (中文版)cento6.5环境安装:cd ~mkdir phalconc ...

  7. Rectangle 暴力枚举大法

    frog has a piece of paper divided into n rows and m columns. Today, she would like to draw a rectang ...

  8. bzoj1178 [Apio2009]CONVENTION会议中心 区间dp+贪心

    [Apio2009]CONVENTION会议中心 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 1130  Solved: 444[Submit][S ...

  9. [解决] User [dr.who] is not authorized to view the logs for application

    在hadoop集群启用权限控制后,发现job运行日志的ui访问不了, User [dr.who] is not authorized to view the logs for application ...

  10. ZooKeeper管理员指南——部署与管理ZooKeeper

    1.部署 本章节主要讲述如何部署ZooKeeper,包括以下三部分的内容: 系统环境 集群模式的配置 单机模式的配置 系统环境和集群模式配置这两节内容大体讲述了如何部署一个能够用于生产环境的ZK集群. ...