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“对象不支持SET__LICENSES属性或方法”SET__LICENSES is not a function”

    Lodop中的方法如果书写错误,就会报错:“对象不支持XXX属性或方法”调试JS会报错”SET__LICENSES is not a function” LODOP.SET_LICENSES是加注册语 ...

  2. Js 布尔值操作符

    在js中,逻辑与(&&) 和 逻辑或(||)可以对任意的数据类型进行操作,而在高级程序设计中只给出了一系列的规则,并没有进行解释,所以经常记不住.在读其它书籍的时候,读到了它的原理,其 ...

  3. Windows下安装Ubuntu 16.04双系统

    本文已有更新:新文章 [2016-05-09 更新说明: ①:我原本写的Ubuntu 16.04安装博客中在安装系统时,在引导项部分,有一点问题没有注意到,感谢@小段阿誉的指出,在下面我有了说明: ② ...

  4. 性能测试工具 Locust

    https://docs.locust.io/en/latest/quickstart.html

  5. L - Vases and Flowers HDU - 4614 线段树+二分

    题意 给出一排空花瓶 有两种操作  1是 从A花瓶开始放F朵花 如果当前瓶有花就跳过前往下一个 直到花用完或者 瓶子到了最后一个为止 输出 成功放花的第一个和最后一个  如果没有输出 can not. ...

  6. Codeforces1063D Candies for Children 【分类讨论】【暴力】

    题目分析: 首先要想两个暴力,一个的时间复杂度是$O(n^2)$,另一个是$O([\frac{n}{k}])$的. $n^2$的暴力可以枚举两段,一段有$i$个取两个的小朋友,一段有$j$个取两个的小 ...

  7. 洛谷3705 [SDOI2017] 新生舞会 【01分数规划】【KM算法】

    题目分析: 裸题.怀疑$ O(n^4log{n}) $跑不过,考虑Edmonds-Karp优化. 代码: #include<bits/stdc++.h> using namespace s ...

  8. 【XSY2524】唯一神 状压DP 矩阵快速幂 FFT

    题目大意 给你一个网格,每个格子有概率是\(1\)或是\(0\).告诉你每个点是\(0\)的概率,求\(1\)的连通块个数\(\bmod d=0\)的概率. 最开始所有格子的概率相等.有\(q\)次修 ...

  9. python3 列表list

    列表用中括号表示[]: list()创建一个列表: 是可变的: 可以被迭代,也可以被切片: +组合列表,*重复列表: 可以使用del删除元素,del L[index]; 方法: append(obj) ...

  10. 【BZOJ5213】[ZJOI2018]迷宫(神仙题)

    [BZOJ5213][ZJOI2018]迷宫(神仙题) 题面 BZOJ 洛谷 题解 首先可以很容易的得到一个\(K\)个点的答案. 构建\(K\)个点分别表示\(mod\ K\)的余数.那么点\(i\ ...