-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path496.下一个更大元素-i.java
56 lines (48 loc) · 1.58 KB
/
496.下一个更大元素-i.java
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
/*
* @lc app=leetcode.cn id=496 lang=java
*
* [496] 下一个更大元素 I
*/
// @lc code=start
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
// mine
// Map<Integer, Integer> nextGreaterMap = new HashMap<Integer, Integer>();
// Stack<Integer> stack = new Stack<Integer>();
// for (int num : nums2) {
// while (!stack.isEmpty()) {
// int ele = stack.peek();
// if (ele < num) {
// stack.pop();
// nextGreaterMap.put(ele, num);
// } else {
// break;
// }
// }
// stack.add(num);
// }
// while (!stack.isEmpty()) {
// int ele = stack.pop();
// nextGreaterMap.put(ele, -1);
// }
// int[] result = new int[nums1.length];
// for (int i = 0 ; i < nums1.length ; i++) {
// result[i] = nextGreaterMap.get(nums1[i]);
// }
// return result;
// Solution from Nick White
HashMap<Integer, Integer> nextGreatest = new HashMap<Integer, Integer>();
Stack<Integer> stack = new Stack<Integer>();
for (Integer num : nums2) {
while (!stack.isEmpty() && stack.peek() < num) {
nextGreatest.put(stack.pop(), num);
}
stack.push(num);
}
for (int i = 0 ; i < nums1.length ; i++) {
nums1[i] = nextGreatest.getOrDefault(nums1[i], -1);
}
return nums1;
}
}
// @lc code=end