(栈)leetcode496. Next Greater Element I
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:
- All elements in
nums1
andnums2
are unique. - The length of both
nums1
andnums2
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。
注意:
nums1
和nums2
中所有元素是唯一的。nums1
和nums2
的数组大小都不超过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的更多相关文章
- Leetcode496.Next Greater Element I下一个更大的元素1
给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值. nums1 中数字 x 的下一个更 ...
- [LeetCode] Next Greater Element II 下一个较大的元素之二
Given a circular array (the next element of the last element is the first element of the array), pri ...
- [LeetCode] Next Greater Element I 下一个较大的元素之一
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- 下一个更大的数 Next Greater Element
2018-09-24 21:52:38 一.Next Greater Element I 问题描述: 问题求解: 本题只需要将nums2中元素的下一个更大的数通过map保存下来,然后再遍历一遍nums ...
- [leetcode]496. Next Greater Element I下一个较大元素
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
- LeetCode——Next Greater Element I
LeetCode--Next Greater Element I Question You are given two arrays (without duplicates) nums1 and nu ...
- [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 ...
- [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 ...
- LeetCode 503. 下一个更大元素 II(Next Greater Element II)
503. 下一个更大元素 II 503. Next Greater Element II 题目描述 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 ...
随机推荐
- Jsoup的使用
http://caidongrong.blog.163.com/blog/static/21424025220139292525874/
- Oracle 查询两个时间段内的所有日期列表
1.查询某时间段内日期列表 select level,to_char(to_date('2013-12-31','yyyy-mm-dd')+level-1,'yyyy-mm-dd') as date_ ...
- 【数学建模】day02-整数规划
基本类似于中学讲的整数规划--线性规划中变量约束为整数的情形. 目前通用的解法适合整数线性规划.不管是完全整数规划(变量全部约束为整数),还是混合整数规划(变量既有整数又有实数),MATLAB都提供了 ...
- git 回退版本
回滚到指定的版本 git reset --hard e377f60e28c8b84158 强制提交 git push -f origin master
- BZOJ3133[ballmachine]——倍增+优先队列
题目描述 有一个装球机器,构造可以看作是一棵树.有下面两种操作: 从根放入一个球,只要下方有空位,球会沿着树滚下.如果同时有多个点可以走,那么会选择编号最小的节点所在路径的方向.比如依次在树根4放2个 ...
- 【AGC018F】Two Trees 构造 黑白染色
题目描述 有两棵有根树,顶点的编号都是\(1\)~\(n\). 你要给每个点一个权值\(a_i\),使得对于两棵树的所有顶点\(x\),满足\(|x\)的子树的权值和\(|=1\) \(n\leq 1 ...
- 【XSY1545】直径 虚树 DP
题目大意 给你一棵\(n\)个点的树,另外还有\(m\)棵树,第\(i\)棵树与原树的以\(r_i\)为根的子树形态相同.这\(m\)棵树之间也有连边,组成一颗大树.求这棵大树的直径长度. \(n ...
- LG P2473 [SCOI2008]奖励关
题目链接:P2473 [SCOI2008]奖励关 题意:有n个宝物 每次等概率抛出其中之一一共抛出k次每个宝物有一个价值 和一个前提集合只有集齐了集合中的所有宝物 才可以领取这个宝物 范围:1 < ...
- Shell基础 - Bash基础功能
历史命令 history选项: -c 清空历史命令 -w 立即保存历史命令Linux 下输入过的历史命令,都会保存在根目录下的:~/root/.bash_history 文件中默认保存 1000 条, ...
- Linux 日志分析脚本
#### 以下代码,若出现无法使用,请根据底下图片,更改参数.根据 apache 日志格式修改 查看 apache 进程ps aux | grep httpd | grep -v grep | wc ...