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 ,其中nums1 是 nums2 的子集。找到 nums1 中每个元素在 nums2 中的下一个比其大的值。

nums1 中数字 x 的下一个更大元素是指 x 在 nums2 中对应位置的右边的第一个比 x 大的元素。如果不存在,对应位置输出-1。

示例 1:

输入: nums1 = [4,1,2], nums2 = [1,3,4,2].
输出: [-1,3,-1]
解释:
对于num1中的数字4,你无法在第二个数组中找到下一个更大的数字,因此输出 -1。
对于num1中的数字1,第二个数组中数字1右边的下一个较大数字是 3。
对于num1中的数字2,第二个数组中没有下一个更大的数字,因此输出 -1。

示例 2:

输入: nums1 = [2,4], nums2 = [1,2,3,4].
输出: [3,-1]
解释:
  对于num1中的数字2,第二个数组中的下一个较大数字是3。
对于num1中的数字4,第二个数组中没有下一个更大的数字,因此输出 -1。

注意:

  1. nums1nums2中所有元素是唯一的。
  2. nums1nums2 的数组大小都不超过1000。

这个要审题,这个说的是该数的右边的第一个比它更大的数,而不是比它大的数的数量。。。

于是可以用栈,同时用map计数,(unordered_map更好,原理是哈希表)

参考内容:

 

解法三:栈和哈希表(数字与右边更大数映射)

解题思路:

理解一:这道题要解决的问题比较简单。一,找到findNums中的数字在nums中的位置;二,寻找右边第一个更大的数字。

理解二(解法三):一,先找出在muns中每一个元素的右边更大的数字;二,判断findNums中的数字是否在nums中,然后取得相应的右边第一个更大的数字或赋值-1。思路的改变,带来解法的改变。

要点:如何利用已有的数据结构,快速解决这两个问题?

(1)先找出在muns中每一个元素的右边更大的数字:哈希表 和 栈

#生长点#
---为什么用栈来做比较更快?或者为什么这里可以用上栈结构?
举个例子:1 3 8 2 4 6 9
用栈比较时:
1)1入栈,接着扫描到3,因为3比1大,所以1——>3,
2)然后1出栈,3进栈,接着扫描到8,因为8比3大,所以3——>8
3)然后3出栈,8进栈,接着扫描到2,2不大于8,2进栈,2成为新的栈顶元素
4)接着扫描到4,因为4比2大,所以2——>4,然后2出栈,此时栈顶元素为8,因4不大于8,4进栈,继续扫描
5)接着扫描到6,因为6比4大,所以4——>6,然后4出栈,此时栈顶元素为8,因6不大于8,6进栈,继续扫描
6)接着扫描到9,因为9比6大,所以6——>9,然后6出栈,此时栈顶元素为8,因9比8大,8——>9,然后8出栈,此时栈空
7)9进栈,栈只有一个元素,后面没有更大的元素了。 注:栈的特点体现在寻找 数字8 的右边第一个更大的 数字9 上面。

参考链接:https://www.cnblogs.com/paulprayer/p/10169532.html#_label3

C++ 代码:

class Solution {
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
stack<int> s;
map<int,int> mp;
for(int i = ;i < nums.size(); i++){
while(!s.empty() && s.top() < nums[i]){
mp[s.top()] = nums[i];
s.pop();
}
s.push(nums[i]);
}
vector<int> res;
for(int i = ; i < findNums.size(); i++){
if(mp.count(findNums[i]))
res.push_back(mp[findNums[i]]);
else
res.push_back(-);
}
return res;
}
};

(栈)leetcode496. Next Greater Element I的更多相关文章

  1. Leetcode496.Next Greater Element I下一个更大的元素1

    给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值. nums1 中数字 x 的下一个更 ...

  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] 496. Next Greater Element I 下一个较大的元素 I

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

  8. [LeetCode] 503. Next Greater Element II 下一个较大的元素 II

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

  9. LeetCode 503. 下一个更大元素 II(Next Greater Element II)

    503. 下一个更大元素 II 503. Next Greater Element II 题目描述 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 ...

随机推荐

  1. Lodop部署web网站 客户端本地打印角色

    Lodop用于客户端本地打印,部署到web网站非常简单,此博文介绍的是混合部署方式,该方式兼容所有浏览器,当浏览器支持np插件的时候,使用Lodop插件方式,浏览器不支持np插件,会用C-Lodop服 ...

  2. LODOP.FORMAT格式转换【回调和直接返回值】

    Lodop中有一些格式转换函数,这些函数和其他众多函数一样,c-lodop需要使用回调函数On_Return返回,Lodop插件方式直接返回,通常混合部署,写法要兼容两个控件.可以用if (LODOP ...

  3. DotNetty 实现 Modbus TCP 系列 (一) 报文类

    本文已收录至:开源 DotNetty 实现的 Modbus TCP/IP 协议 Modbus TCP/IP 报文 报文最大长度为 260 byte (ADU = 7 byte MBAP Header ...

  4. endnote插入文献时出现{,#}这样的乱码

    1)在每次插入文献后,再点击一下Bibliography里面的Update Citation and Bibliography即可: (2)较好的解决方法也较为简单,只需要再一次进入Word中的End ...

  5. c++ string类型的定义及方法

    1.c++ 有两种风格的字符串形式  c风格字符串  定义及初始化  char a[]={'h','e','l','l','o','\0'}  或者  char a[]="hello&quo ...

  6. Html5 拖拽api

    拖拽要有两个元素,一个是要拖动的元素,一个是要放置到的元素. 1, 在默认情况下, 只有图片和文字是可以拖拽的,其它元素都不可以.因此要想使一个元素可拖动,必须设置它的draggable 属性为tru ...

  7. codeforces496C

    Removing Columns CodeForces - 496C You are given an n × m rectangular table consisting of lower case ...

  8. 实验吧 WEB 貌似有点难

    错误!你的IP不在允许列表之内! 提示:代码审计 这个提示可谓是非常良心了,一看源代码是一个识别ip地址的东西,如果IP为1.1.1.1那么就会得到KEY. 第一个if是判断是否有client-ip ...

  9. BZOJ1014[JSOI2008]火星人——非旋转treap+二分答案+hash

    题目描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 ...

  10. LOJ6432 [PKUSC2018] 真实排名 【组合数】

    题目分析: 做三个指针然后预处理阶乘就行. 题目代码: #include<bits/stdc++.h> using namespace std; ; ; int n,k; struct n ...