forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_496.java
32 lines (26 loc) · 896 Bytes
/
_496.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
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
public class _496 {
public static class Solution1 {
public int[] nextGreaterElement(int[] findNums, int[] nums) {
Stack<Integer> stack = new Stack();
Map<Integer, Integer> map = new HashMap();
for (int i = 0; i < nums.length; i++) {
while (!stack.isEmpty() && nums[i] > stack.peek()) {
map.put(stack.pop(), nums[i]);
}
stack.push(nums[i]);
}
while (!stack.isEmpty()) {
map.put(stack.pop(), -1);
}
int[] result = new int[findNums.length];
for (int i = 0; i < findNums.length; i++) {
result[i] = map.get(findNums[i]);
}
return result;
}
}
}