Leetcode每日一题:134.gas-station(加油站)
思路:就按题目所给的要求来,到一个站点,就减去消耗的油,如果没油了,返回false,否则,加上该站点加的油,重复直到开到入口点;注意到最后一个站点时,要把下一个站点设为0;
int init = 0;
int flag = true;
void dfs(vector<int> &gas, vector<int> &cost, int len, int now, int remain) //从enter口开始
{//开到下一个点,消耗了油remain -= cost[now];//如果没油了,返回if (remain < 0){flag = false;return;}int next = 0;if (now != len - 1)next = now + 1;//如果开到了起始点,则说明满足条件if (next == init){flag = true;return;}//加上下一个点加的油remain += gas[next];dfs(gas, cost, len, next, remain);
}int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{int len = gas.size();for (int i = 0; i < len; i++){if (gas[i] >= cost[i]){init = i;dfs(gas, cost, len, i, gas[i]);if (flag == true){return init;}}}return -1;
}
Leetcode每日一题:134.gas-station(加油站)相关推荐
- 134. Gas Station加油站
[抄题]: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...
- 134. Gas Station 加油站
在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其中的一个加 ...
- LeetCode 134 Gas Station
LeetCode 134 Gas Station 水题,暴力一下就ok class Solution { public:int tag[100005];int sum[100005];int canC ...
- LeetCode 134. Gas Station
LeetCode 134. Gas Station Solution1:我的答案,无数次试错得到的,不成系统,没有思路.. 时间复杂度O(n2)O(n2)O(n^2) class Solution { ...
- leetcode每日刷题计划-简单篇day8
leetcode每日刷题计划-简单篇day8 今天是纠结要不要新买手机的一天QAQ想了想还是算了吧,等自己赚钱买,加油 Num 70 爬楼梯 Climbing Stairs class Solutio ...
- Leetcode每日一题:171.excel-sheet-column-number(Excel表列序号)
思路:就是168题的反命题,进制的方式完美解决: Leetcode每日一题:168.excel-sheet-column-title(Excel表名称) class Solution {public: ...
- 【LeetCode每日一题】1723. 完成所有工作的最短时间
[LeetCode每日一题]1723. 完成所有工作的最短时间 [1] 1723. 完成所有工作的最短时间 [2] 473. 火柴拼正方形 [1] 1723. 完成所有工作的最短时间 题目: 给你一个 ...
- leetcode每日一题--雀巢原理;抽屉算法;Quorum机制;分布式应用
leetcode每日一题 539. 最小时间差 示例 1: 输入:timePoints = ["23:59","00:00"] 输出:1 示例 2: 输入:ti ...
- LeetCode每日一题——1812. 判断国际象棋棋盘中一个格子的颜色
LeetCode每日一题系列 题目:1812. 判断国际象棋棋盘中一个格子的颜色 难度:简单 文章目录 LeetCode每日一题系列 题目 示例 思路 题解 题目 给你一个坐标 coordinates ...
- 【LeetCode每日一题】——109.有序链表转换二叉搜索树
文章目录 一[题目类别] 二[题目难度] 三[题目编号] 四[题目描述] 五[题目示例] 六[题目提示] 七[解题思路] 八[时间频度] 九[代码实现] 十[提交结果] 一[题目类别] 二叉树 二[题 ...
最新文章
- Spring Cloud限流详解(附源码)
- Python基础案例(一)
- LeetCode 1721. 交换链表中的节点(快慢指针)
- ubuntu下使用python将ppt转成图片_Ubuntu下使用Python实现游戏制作中的切分图片功能...
- 开源个性化内容推荐引擎_您是哪种开源个性类型?
- c# 解析Xml文件
- 2020年华为鲲鹏产业体系研究深度报告
- SpringBoot2.4.2+Vue集成Activiti6流程引擎
- 基于Proteus仿真8253音乐发生器
- 数据可视化工具在医疗领域的应用
- 期待只在最美的时光遇见你
- 原生js实现拖拽效果,js拖拽原理
- 同步AOKP源码的方法
- 通俗讲解CDN是什么
- 爬虫实战——中国天气网数据
- 2022 年最值得学习的 10 种编程语言 [更新]
- 关于瞎报Access violation writing location异常的瞎解决方法
- 30岁后,你拿什么养活自己
- python-(6)常用的字符判断和转换以及开头结尾匹配
- 数据库系统概念 | 第六章:形式化关系查询语言 | 含带答案习题