编程练习(第九周)
来源:互联网 发布:it管理软件 编辑:程序博客网 时间:2024/06/04 19:38
题目来源于:https://leetcode.com
134. Gas Station
DescriptionHintsSubmissionsSolutions
- Total Accepted: 81981
- Total Submissions: 283411
- Difficulty: Medium
- Contributor: LeetCode
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.
题解:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { for(int i = 0; i < gas.size(); i++) { gas[i] -= cost[i]; } int sum = 0; int result = 0; int n = gas.size(); for(int i = 0; i < n * 2 - 1; i++) { sum += gas[i % n]; if(sum < 0) { result = i + 1; if(result >= n) { return -1; } sum = 0; } } return result; }
0 0
- 编程练习(第九周)
- (一一四)第九章编程练习
- 编程练习 第九章
- 第九章编程练习(3)
- 模板元编程第九章练习(非答案)
- 第九周练习
- oj第九周练习
- 第九周练习--等比数列
- 记录——《C Primer Plus (第五版)》第九章编程练习第九题
- python核心编程--第九章 9.11 练习
- Python核心编程 第九章练习
- python核心编程第九章练习
- 第九章 函数 编程练习个人答案
- C++ primer plus 第九章编程练习
- 第九周练习 阅读程序
- oj第九周练习 等比数列
- 第九周练习--切面条
- 第九周练习--大赛积分
- (一)JUnit概述及一个简单例子
- 继承的运用
- 设计模式--策略模式的python实现
- 滤波算法
- 扫雷小游戏简单易懂
- 编程练习(第九周)
- Linux块设备驱动(三)————块设备驱动程序的框架
- Linux系统下安装rz/sz命令及使用说明
- [状压DP 边双连通分量] BZOJ 3590 [Snoi2013]Quare
- 深度学习:感知机perceptron
- MyBatis 3.2.x版本在并发情况下可能出现的bug及解决办法
- 少年,是时候换种更优雅的方式部署你的php代码了
- 【LeetCode】415. Add Strings,用string模拟加法
- CF- 798A. Mike and palindrome