Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
feilong-sys
algorithm-journey
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
algorithm-journey
/
src
/
class049
/
Code04_GasStation.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
33 lines (30 loc) · 1.27 KB
main
Breadcrumbs
algorithm-journey
/
src
/
class049
/
Code04_GasStation.java
Copy path
Top
File metadata and controls
Code
Blame
33 lines (30 loc) · 1.27 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package
class049
;
// 加油站
// 在一条环路上有 n 个加油站,其中第 i 个加油站有汽油 gas[i] 升。
// 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升
// 你从其中的一个加油站出发,开始时油箱为空。
// 给定两个整数数组 gas 和 cost ,如果你可以按顺序绕环路行驶一周
// 则返回出发时加油站的编号,否则返回 -1
// 如果存在解,则 保证 它是 唯一 的。
// 测试链接 : https://leetcode.cn/problems/gas-station/
public
class
Code04_GasStation
{
public
static
int
canCompleteCircuit
(
int
[]
gas
,
int
[]
cost
) {
int
n
=
gas
.
length
;
// 本来下标是0..n-1,但是扩充到0..2*n-1,i位置的余量信息在(r%n)位置
// 窗口范围是[l, r),左闭右开,也就是说窗口是[l..r-1],r是到不了的位置
for
(
int
l
=
0
,
r
=
0
,
sum
;
l
<
n
;
l
=
r
+
1
,
r
=
l
) {
sum
=
0
;
while
(
sum
+
gas
[
r
%
n
] -
cost
[
r
%
n
] >=
0
) {
// r位置即将右扩,窗口会变大
if
(
r
-
l
+
1
==
n
) {
// 此时检查是否已经转了一圈
return
l
;
}
// r位置进入窗口,累加和加上r位置的余量
sum
+=
gas
[
r
%
n
] -
cost
[
r
%
n
];
// r右扩,窗口变大了
r
++;
}
}
return
-
1
;
}
}
You can’t perform that action at this time.