[leetcode]496. 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.
题意:
给定数组nums, 又给定它的子集subNums, 求subNums中每个元素在nums中右边第一个较大元素(即Next Greater Element)
subNums = [,1,2] 当扫到元素4, 该元素在 nums = [1,3,,2] 中右边第一个数为2, 并不greater than 4, 返回-1
subNums = [4,,2] 当扫到元素1, 该元素在 nums = [,3,4,2] 中右边第一个数为3, 确实greater than 1, 返回 3
subNums = [4,1,] 当扫到元素2, 该元素在 nums = [1,3,4,] 中右边没有元素, 不存在greater than 2, 返回-1
思路:
两个指针同时扫 subNums 和 nums
用一个boolean变量做标记,若当前 subNums 元素等于当前nums元素,则标记 found = true ,说明找到了corresponding元素。
nums指针继续往右,找greater element, 直到找到符合 found && nums[j] > subNums[i] 条件的元素。否则返回-1
代码一:
class Solution {
public int[] nextGreaterElement(int[] subNums, int[] nums) {
int[] res = new int[subNums.length];
for (int i = 0; i < subNums.length; i++) {
boolean found = false;
int j = 0;
for (; j < nums.length; j++) {
if (found && nums[j] > subNums[i]) {
res[i] = nums[j];
break;
}
if (found && nums[j] < subNums[i]) {
res[i] = -1;
}
if (nums[j] == subNums[i]) {
found = true;
}
}
if (j == nums.length) {
res[i] = -1;
}
}
return res;
}
}
另外一个思路就是用单调栈 + HashMap
为何用单调栈? 为任意一个元素找左边和右边第一个比自己大/小的位置,用单调栈。
先只take care of nums : 从右往左扫nums, 用Stack维护递减栈,留下波峰,剔除波谷。比较栈顶元素跟即将入栈元素大小,
用HashMap来记录其比较结果。再take care of nums: 扫一遍subNums,在HashMap中找到对应的value,返回即可。
subNums = [4,1,2], nums = [1,0,3,4,2]
Stack HashMap
^ [2] 2 | -1
^ [4] 4 | -1
^ [4, 3] 3 | 4
^ [4, 3, 0] 0 | 3
^ [4, 3, 1] 1 | 3
代码二:
public int[] nextGreaterElement(int[] subNums, int[] nums) {
int[] res = new int[subNums.length];
Stack<Integer> stack = new Stack<>();
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = nums.length - 1; i >= 0; i--) {
while (!stack.empty() && nums[i] > stack.peek()) {
stack.pop();
}
if (stack.empty()) {
map.put(nums[i], -1);
} else {
map.put(nums[i], stack.peek());
}
stack.push(nums[i]);
}
for (int i = 0; i < subNums.length; i++) {
res[i] = map.get(subNums[i]);
}
return res;
}
[leetcode]496. Next Greater Element I下一个较大元素的更多相关文章
- [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 ...
- 496 Next Greater Element I 下一个更大元素 I
给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值.nums1 中数字 x 的下一个更大 ...
- [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 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它 ...
- 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 III 下一个较大的元素之三
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...
- [LeetCode] Next Greater Element I 下一个较大的元素之一
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of n ...
随机推荐
- IT职业发展攻略(技术仅是工具而已)
时光飞逝,我事业中第一个十年就快结束了.在这十年里,让我收获了很多,今天想与大家分享一下,我在 IT 职场方面的一些个人经验,不一定对每个人都实用,请大家仅作参考吧. 大家既然都是做技术的,那我们不妨 ...
- javascript继承之原型式继承(四)
javascript之父道格拉斯在2006年给出了这样一串代码,来实现继承. function object(o) { function F() { } F.prototype = o; return ...
- php trim() 函数实例讲解
php trim() 函数移除字符串两侧的空白字符或其他预定义字符,本文章向码农介绍php trim() 函数的使用方法和实例,感兴趣的码农可以参考一下. 定义和用法 trim() 函数移除字符串两侧 ...
- MySQL数据库索引(上)
上一篇回顾: 1.数据页由七部分组成,包括File Header(描述页的信息).Page Header(描述数据的信息).Infimum + Supremum(页中的虚拟数据最大值和最小值).Use ...
- WARN hdfs.DFSClient: Caught exception java.lang.InterruptedException
Hadoop 2.7.4 The reason is this: originally, DataStreamer::closeResponder always prints a warning ab ...
- Linux系统下MongoDB的简单安装与基本操作
这篇文章主要介绍了Linux系统下MongoDB的简单安装与基本操作,需要的朋友可以参考下 Mongo DB ,是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式,备 ...
- swagger配置
1.pom.xml <!--swagger2--> <dependency> <groupId>io.springfox</groupId> <a ...
- 有关Firefox/Chrome的问题汇总
安装的附加组件因未经验证而被 Firefox 禁用,我该怎么办 如果您已安装的附加组件因未经验证而被禁用了,建议您联系附加组件开发者或提供给您附加组件的人,看看他们能不能提供新版经过验证的附加组件.您 ...
- VMware vCenter Server 6.5.0 U1g
VMware vCenter Server 6.5.0 U1gName: VMware-VCSA-all-6.5.0-8024368.iso Release Date: 2018-03-20 Buil ...
- vmware桥接模式无法上网
环境:本机win10系统,ip地址固定,(估计存在vlan网络), 状况:vmware的nat模式可以上网,桥接模式不能上网, 解决办法:找网管把本机设置成dhcp模式,才行了