[LeetCode] 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.
这道题给了我们一个数组,又给了该数组的一个子集合,让我们求集合中每个数字在原数组中右边第一个较大的数字。参考题目中给的例子,题意不难理解,既然这次难度标识为Easy,想必不是一道太难的题。二话不说,先上无脑暴力搜索,遍历子集合中的每一个数字,然后在原数组中找到这个数字,然后向右遍历,找到第一个大于该数字的数即可,参见代码如下:
解法一:
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
vector<int> res(findNums.size());
for (int i = ; i < findNums.size(); ++i) {
int j = , k = ;
for (; j < nums.size(); ++j) {
if (nums[j] == findNums[i]) break;
}
for (k = j + ; k < nums.size(); ++k) {
if (nums[k] > nums[j]) {
res[i] = nums[k];
break;
}
}
if (k == nums.size()) res[i] = -;
}
return res;
}
};
我们来对上面的方法稍做优化,我们用哈希表先来建立每个数字和其坐标位置之间的映射,那么我们在遍历子集合中的数字时,就能直接定位到该数字在原数组中的位置,然后再往右边遍历寻找较大数即可,参见代码如下:
解法二:
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
vector<int> res(findNums.size());
unordered_map<int, int> m;
for (int i = ; i < nums.size(); ++i) {
m[nums[i]] = i;
}
for (int i = ; i < findNums.size(); ++i) {
res[i] = -;
int start = m[findNums[i]];
for (int j = start + ; j < nums.size(); ++j) {
if (nums[j] > findNums[i]) {
res[i] = nums[j];
break;
}
}
}
return res;
}
};
下面这种方法使用了哈希表和栈,但是这里的哈希表和上面的不一样,这里是建立每个数字和其右边第一个较大数之间的映射,没有的话就是-1。我们遍历原数组中的所有数字,如果此时栈不为空,且栈顶元素小于当前数字,说明当前数字就是栈顶元素的右边第一个较大数,那么建立二者的映射,并且去除当前栈顶元素,最后将当前遍历到的数字压入栈。当所有数字都建立了映射,那么最后我们可以直接通过哈希表快速的找到子集合中数字的右边较大值,参见代码如下:
解法三:
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
vector<int> res;
stack<int> st;
unordered_map<int, int> m;
for (int num : nums) {
while (!st.empty() && st.top() < num) {
m[st.top()] = num; st.pop();
}
st.push(num);
}
for (int num : findNums) {
res.push_back(m.count(num) ? m[num] : -);
}
return res;
}
};
类似题目:
参考资料:
https://leetcode.com/problems/next-greater-element-i
https://leetcode.com/problems/next-greater-element-i/discuss/97676/java-solution-with-hashmap
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Next Greater Element I 下一个较大的元素之一的更多相关文章
- [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 III 下一个较大的元素之三
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...
- [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] 556. Next Greater Element III 下一个较大的元素 III
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...
- 503 Next Greater Element II 下一个更大元素 II
给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它 ...
- 496 Next Greater Element I 下一个更大元素 I
给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值.nums1 中数字 x 的下一个更大 ...
- Leetcode496.Next Greater Element I下一个更大的元素1
给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值. nums1 中数字 x 的下一个更 ...
- [leetcode]496. Next Greater Element I下一个较大元素
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
随机推荐
- swift 相关小随笔
关键词 typealias 对已经存在的类重命名 let 修饰不可变值 var 修饰可变的值 lazy 懒加载修饰符,用到的时候才会加载 convenience 原方法的备用方法,方法一致,但是 ...
- CSS选取第n个标签元素
最近做一个项目,碰到这样的需求,需要选取某个元素的倒数第几个标签元素,想让他显示不同的样式 1.first-child first-child表示选择列表中的第一个标签.例如:li:first-chi ...
- 移动前端的html5 head 头标签
DOCTYPE DOCTYPE(Document Type),该声明位于文档中最前面的位置,处于 html 标签之前,此标签告知浏览器文档使用哪种 HTML 或者 XHTML 规范. 使用 HTML5 ...
- MySQL之连接查询
主要是多表查询和连接查询
- Alpha冲刺No.4
冲刺Day4 一.站立式会议 本来还想今天下午好好弄弄安卓开发,结果计划赶不上变化.(不存在的) 完成备忘录设计,个人界面设计 二.实际项目进展 搞了404(安卓和ssm的连接),好像还是不太行. 备 ...
- Beta预备会议
1. 讨论组长是否重选的议题和结论. 我们小组决定组长更换为林洋洋同学,他Web开发经验比较丰富,对任务的分配会更加明确,由于上一阶段中存在进度偏慢的问题,我们希望在Beta阶段通过更好的分工安排来保 ...
- python中使用flask时遇到的markupsafe._compat包缺失的问题与解决
环境:windows7 + python3.6.0 在尝试使用python的flask时,按照flask的tutorial操作,装好flask.venv后,对tutorial中的hello.py进行运 ...
- HTTP协议形象展现
关于http协议:我们分成几个模块说: http协议: HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统. HTTP协议的主要特点可概括如下: 1.支持客户 ...
- nyoj 孪生素数
孪生素数问题 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 写一个程序,找出给出素数范围内的所有孪生素数的组数.一般来说,孪生素数就是指两个素数距离为2,近的不能再 ...
- TortoiseGit安装与使用
公司的源码是在码云上,平时进行项目源码管理和团队开发都会使用到GIT,花了一天时间才将Git搞明白,这是一个工具,我在这里就简单说一下,其安装使用方法,也是对自己学习的总结;本文章适合于刚接触GIT的 ...