leetcode贪心之gas-station条件决定答案
来源:互联网 发布:牛贝微信淘宝客安装 编辑:程序博客网 时间:2024/05/16 01:21
这个题主要注意俩点:
抓住如果有解, 一定是唯一解 的约束条件
另外如果总油量小于总消耗的油量表明一定无法走完全程
充分说明了题目中给的条件的重要性:
代码:
class Solution {public: int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { int sum=0,total=0,j=-1; for(int i=0;i<gas.size();i++){ sum+=gas[i]-cost[i]; total+=gas[i]-cost[i]; if(sum<0){ j=i; sum=0; } } return total>=0?j+1:-1; }};
0 0
- leetcode贪心之gas-station条件决定答案
- [leetcode][贪心] Gas Station
- leetcode---gas-station---贪心
- leetCode #134 Gas Station 贪心
- leetcode贪心算法:Gas Station
- LeetCode 之 Gas Station
- LeetCode之Gas Station
- leetcode之Gas Station
- leetcode之Gas Station
- leetcode之Gas Station
- LeetCode之Gas Station
- Leetcode之Gas Station
- leetcode之Gas Station
- [leetcode] 134 Gas Station(经典dp || 贪心)
- leetCode解题报告之Gas Station
- LeetCode之Gas Station --环形加油站周游
- LeetCode OJ 之 Gas Station (加油站)
- [LeetCode]Gas Station 最大子序列和,贪心
- Java中常见的几种RuntimeException
- Mathf类
- Android工具类--App相关的辅助类
- 图之关节点和重连通分量
- 如何用ps制作GIF动图
- leetcode贪心之gas-station条件决定答案
- 别踩白块的两种模式
- 【opencv练习13 - File 输入输出】
- 哈希的存取为何快?
- Weex入门与进阶指南
- arm 知识
- 组合数/Lucas定理
- 水仙花数
- 给Android程序员的一些面试建议