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 }}
chenwenhang
AlgorithmPractice
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
AlgorithmPractice
/
src
/
array_and_matrix
/
NextLargerElement.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
78 lines (53 loc) · 2 KB
master
Breadcrumbs
AlgorithmPractice
/
src
/
array_and_matrix
/
NextLargerElement.java
Copy path
Top
File metadata and controls
Code
Blame
78 lines (53 loc) · 2 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package array_and_matrix;
import java.util.HashMap;
import java.util.Stack;
/**
* @Author: Wenhang Chen
* @Description:给定两个 没有重复元素 的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集。找到 nums1 中每个元素在 nums2 中的下一个比其大的值。
* <p>
* nums1 中数字 x 的下一个更大元素是指 x 在 nums2 中对应位置的右边的第一个比 x 大的元素。如果不存在,对应位置输出 -1 。
* <p>
*
* <p>
* 示例 1:
* <p>
* 输入: nums1 = [4,1,2], nums2 = [1,3,4,2].
* 输出: [-1,3,-1]
* 解释:
* 对于num1中的数字4,你无法在第二个数组中找到下一个更大的数字,因此输出 -1。
* 对于num1中的数字1,第二个数组中数字1右边的下一个较大数字是 3。
* 对于num1中的数字2,第二个数组中没有下一个更大的数字,因此输出 -1。
* 示例 2:
* <p>
* 输入: nums1 = [2,4], nums2 = [1,2,3,4].
* 输出: [3,-1]
* 解释:
* 对于 num1 中的数字 2 ,第二个数组中的下一个较大数字是 3 。
* 对于 num1 中的数字 4 ,第二个数组中没有下一个更大的数字,因此输出 -1 。
*
* <p>
* 提示:
* <p>
* nums1和nums2中所有元素是唯一的。
* nums1和nums2 的数组大小都不超过1000。
* @Date: Created in 8:37 6/7/2020
* @Modified by:
*/
public class NextLargerElement {
public int[] nextGreaterElement(int[] findNums, int[] nums) {
Stack<Integer> stack = new Stack<>();
HashMap<Integer, Integer> map = new HashMap<>();
int[] res = new int[findNums.length];
for (int i = 0; i < nums.length; i++) {
while (!stack.empty() && nums[i] > stack.peek())
map.put(stack.pop(), nums[i]);
stack.push(nums[i]);
}
while (!stack.empty())
map.put(stack.pop(), -1);
for (int i = 0; i < findNums.length; i++) {
res[i] = map.get(findNums[i]);
}
return res;
}
}
You can’t perform that action at this time.