题解:
public class Solution {
public int[] nextGreaterElement(int[] findNums, int[] nums) {
/*
Time Complexity:O(N)
Space Complexity:O(N)
*/
if(nums==null || nums.length==0)return new int[]{};
HashMap<Integer,Integer>map=new HashMap<>();
ArrayDeque<Integer>sta=new ArrayDeque<>();
for(int num:nums){
while(!sta.isEmpty() && sta.peek()<num){
map.put(sta.pop(),num);
}
sta.push(num);
}
int[] res=new int[findNums.length];
for(int i=0;i<findNums.length;i++){
res[i]=map.getOrDefault(findNums[i],-1);
}
return res;
}
}