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对应元素的右边第一个比其大的值,不存在则返回-1
思路:将nums2做处理,判断每个元素存在的情况,右边若有比自身大的数存入map,然后直接查找map中的健值对情况。
JAVA CODE:
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
Map<Integer, Integer> map = new HashMap<>();
Stack<Integer> stack = new Stack<>();
for (int num : nums2) {
while (!stack.isEmpty() && stack.peek() < num)
map.put(stack.pop(), num);
stack.push(num);
}
for (int i = 0; i < nums1.length; i++)
nums1[i] = map.getOrDefault(nums1[i], -1);
return nums1;
}
}
Next Greater Element I的更多相关文章
- [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 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 Next Greater Element III
原题链接在这里:https://leetcode.com/problems/next-greater-element-iii/description/ 题目: Given a positive 32- ...
- 496. Next Greater Element I 另一个数组中对应的更大元素
[抄题]: You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subse ...
- 数组和矩阵(3)——Next Greater Element I
https://leetcode.com/problems/next-greater-element-i/#/description You are given two arrays (without ...
- [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 ...
随机推荐
- HTML5_input_file_打开很慢的问题
最近项目中有上传附件的功能,只是在chrome浏览器上面测试,发现上传附件,打开选择框比较慢 原文链接:http://www.foreverpx.cn
- MySQL(七)MySQL常用函数
前言 上一篇给大家介绍了,MySQL常用的操作符其实已经是非常的详细了,现在给大家分享的是MySQL的常用函数.希望对我和对大家都有帮助. 一.字符串函数 1.1.LOWER.lcase(string ...
- apache如何设置缓存
基本介绍 httpd是一个比较经典的web服务器,也就是静态资源服务器,主要用来服务于一些静态的文件,例如css,js,html等文件,所谓的静态文件,也就是不需要通过服务器进行运行的文件. 在使用静 ...
- java开发网易电话面试 一面总结
晚上八点多自己在看视频的时候突然接到杭州来的一个电话,当时觉得很奇怪,突兀,接通之后被告知是杭州网易打来的,没有简单的自我介绍,没有多余的废话,直接入主题,吓得我心里怪紧张的,完全没有准备,但是也没有 ...
- Tomcat session生成算法
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt396 修改TOMCAT 默认的生成SESSION ID的算法和字符长度非常简 ...
- tomcat配置单项HTTPS协议
1.进入到jdk下的bin目录 1)进入cmd窗口,cd进入目录: 2)找到JDK安装bin目录,shift+右击打开命令窗口: 3)如果配置类环境变量,在任意cmd命令窗口都可以: 2.输入 ...
- sql处理null值
IFNULL(expr1,expr2) 如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2.IFNULL()返回一个数字或字符串值. select (case when ...
- vim文本基础
p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; f ...
- 【beta】阶段 第六次 Scrum Meeting
每日任务 1.本次会议为第六次 Meeting会议: 2.本次会议在周六上午大课间,在陆大楼召开,召开本次会议为15分钟. 一.今日站立式会议照片 二.每个人的工作 (有work item 的ID) ...
- java中null的类型匹配
null作为一个特殊的参数匹配为String对象