美文网首页
496. 下一个更大元素 I

496. 下一个更大元素 I

作者: 上杉丶零 | 来源:发表于2019-02-18 21:12 被阅读0次
    class Solution {
        public int[] nextGreaterElement(int[] nums1, int[] nums2) {
            Stack<Integer> iStack = new Stack<Integer>();
            Map<Integer, Integer> iIMap = new HashMap<Integer, Integer>();
    
            for (int i : nums2) {
                while (!iStack.isEmpty() && iStack.peek() < i) {
                    iIMap.put(iStack.pop(), i);
                }
    
                iStack.add(i);
            }
    
            for (int i = 0; i < nums1.length; i++) {
                if (iIMap.containsKey(nums1[i])) {
                    nums1[i] = iIMap.get(nums1[i]);
                } else {
                    nums1[i] = -1;
                }
            }
    
            return nums1;
        }
    }
    
    image.png

    相关文章

      网友评论

          本文标题:496. 下一个更大元素 I

          本文链接:https://www.haomeiwen.com/subject/ilnzeqtx.html