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
Events
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 }}
uhauha2929
LeetCode
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
LeetCode
/
src
/
S501_520
/
S503.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
39 lines (33 loc) · 1.47 KB
master
Breadcrumbs
LeetCode
/
src
/
S501_520
/
S503.java
Copy path
Top
File metadata and controls
Code
Blame
39 lines (33 loc) · 1.47 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
34
35
36
37
38
39
package S501_520;
import java.util.*;
/**
* 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素。数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它的下一个更大的数。如果不存在,则输出 -1。
* <p>
* 示例 1:
* <p>
* 输入: [1,2,1]
* 输出: [2,-1,2]
* 解释: 第一个 1 的下一个更大的数是 2;
* 数字 2 找不到下一个更大的数;
* 第二个 1 的下一个最大的数需要循环搜索,结果也是 2。
* 注意: 输入数组的长度不会超过 10000。
*/
public class S503 {
public int[] nextGreaterElements(int[] nums) {
int len = nums.length;
int[] res = new int[len];
Arrays.fill(res, -1);
ArrayDeque<Integer> stack = new ArrayDeque<>(); // 使用ArrayDeque双端队列代替栈效率高
for (int i = 0; i < len * 2; i++) {
int num = nums[(i + len) % len];
while (!stack.isEmpty() && num > nums[stack.peek()]) {
res[stack.pop()] = num; // 不同于Solution496,这里栈里存放的是下标,map里不会重复
}
if (i < len) stack.push(i);
}
return res;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(new S503().nextGreaterElements(new int[]{1, 3, 1, 2, 1})));
}
}
You can’t perform that action at this time.