You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.

The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.

Example 1:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
For number 1 in the first array, the next greater number for it in the second array is 3.
For number 2 in the first array, there is no next greater number for it in the second array, so output -1.

Example 2:

Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
For number 2 in the first array, the next greater number for it in the second array is 3.
For number 4 in the first array, there is no next greater number for it in the second array, so output -1.

Note:

  1. All elements in nums1 and nums2 are unique.
  2. The length of both nums1 and nums2 would not exceed 1000.

分析:分别找到nums1中在nums2对应元素的右边第一个比其大的值,不存在则返回-1

思路:将nums2做处理,判断每个元素存在的情况,右边若有比自身大的数存入map,然后直接查找map中的健值对情况。

JAVA CODE:

class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
Map<Integer, Integer> map = new HashMap<>();
Stack<Integer> stack = new Stack<>();
for (int num : nums2) {
while (!stack.isEmpty() && stack.peek() < num)
map.put(stack.pop(), num);
stack.push(num);
}
for (int i = 0; i < nums1.length; i++)
nums1[i] = map.getOrDefault(nums1[i], -1);
return nums1;
}
}

Next Greater Element I的更多相关文章

  1. [LeetCode] Next Greater Element III 下一个较大的元素之三

    Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...

  2. [LeetCode] Next Greater Element II 下一个较大的元素之二

    Given a circular array (the next element of the last element is the first element of the array), pri ...

  3. [LeetCode] Next Greater Element I 下一个较大的元素之一

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  4. 下一个更大的数 Next Greater Element

    2018-09-24 21:52:38 一.Next Greater Element I 问题描述: 问题求解: 本题只需要将nums2中元素的下一个更大的数通过map保存下来,然后再遍历一遍nums ...

  5. [leetcode]496. Next Greater Element I下一个较大元素

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

  6. LeetCode——Next Greater Element I

    LeetCode--Next Greater Element I Question You are given two arrays (without duplicates) nums1 and nu ...

  7. LeetCode Next Greater Element III

    原题链接在这里:https://leetcode.com/problems/next-greater-element-iii/description/ 题目: Given a positive 32- ...

  8. 496. Next Greater Element I 另一个数组中对应的更大元素

    [抄题]: You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subse ...

  9. 数组和矩阵(3)——Next Greater Element I

    https://leetcode.com/problems/next-greater-element-i/#/description You are given two arrays (without ...

  10. [LeetCode] 496. Next Greater Element I 下一个较大的元素 I

    You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...

随机推荐

  1. Spring4整合quartz2.2.3,quartz动态任务

    Spring4整合quartz2.2.3,quartz动态任务 >>>>>>>>>>>>>>>>> ...

  2. linux命令详解(一)netstat

    今天在使用linux的时候,要查看端口号,但是不知道要使用哪一个命令所以就学习了一下,原来是使用netstat,接下来给大家一起来学习. 一.netstat介绍 1.1.简介 Netstat 命令用于 ...

  3. basepath的作用 (转)

    转自:http://blog.csdn.net/randomnet/article/details/8630754   在谈basePath之前,先来讨论一下相对路径与绝对路径的区别.    相对路径 ...

  4. jQuery控制a标签不可点击 不跳转

    jquery禁用a标签方法1 01 02 03 04 05 06 07 08 09 10 11 12 $(document).ready(function () {         $("a ...

  5. Winform控件输入的字母转换成大写

    private void textBoxHbh_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar >= 'a' & ...

  6. 转:【Java集合源码剖析】Hashtable源码剖析

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/36191279 Hashtable简介 Hashtable同样是基于哈希表实现的,同样每个元 ...

  7. 201521123022 《Java程序设计》第三周学习总结

    1.本周学习总结 2.书面作业 Q1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; pu ...

  8. 【Intellij Idea】设置JDK

    1,File-->Project Structure 说明:可以设置整个工程的JDK,也设置每个modules的JDK,但是一般整个工程的JDK. 2,或者通过快捷键:ctrl+alt+shif ...

  9. 多线程面试题系列(8):经典线程同步 信号量Semaphore

    前面介绍了关键段CS.事件Event.互斥量Mutex在经典线程同步问题中的使用.本篇介绍用信号量Semaphore来解决这个问题. 首先也来看看如何使用信号量,信号量Semaphore常用有三个函数 ...

  10. Mybatis第五篇【Mybatis与Spring整合】

    Mybatis与Spring整合 既然我们已经学了Mybatis的基本开发了,接下来就是Mybatis与Spring的整合了! 以下使用的是Oracle数据库来进行测试 导入jar包 aopallia ...