496. Next Greater Element I
来源:互联网 发布:关于美食的评价知乎 编辑:程序博客网 时间:2024/06/04 16:20
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.
思路:
方法一:最笨的方法可以循环遍历两个数组,但是时间复杂的太高。
方法二:对第二个数组nums中的每个元素,找到它的Next Greater Element,即对应的下一个比它大的元素,形成<key,value>对并保存在HashMap中。然后遍历第一个数组findNums中每个元素,从HashMap中找到对应的Next Greater Element,存进数组对应位置。
这里用到了栈。【 栈 】正常循环的情况下,数组的滚动是向后的,引入栈后,则可以向前滚动,这样就能够解决一些局部的问题(比如寻找相邻的大的数字)。同时栈还可以删除没有价值的元素,具备遗忘功能。用到了栈的isEmpty()方法和HashMap的put()方法、getOrDefault()方法。
public class Solution { public int[] nextGreaterElement(int[] findNums, int[] nums) { Map<Integer,Integer> map = new HashMap<>(); Stack<Integer> stack = new Stack<>(); for(int num:nums){ while(!stack.isEmpty()&&stack.peek()<num){//用while而不能用if map.put(stack.pop(),num); } stack.push(num); } for (int i=0;i<findNums.length;i++){ findNums[i]=map.getOrDefault(findNums[i],-1); } return findNums; }}
程序的第6行应使用while判断并循环,不能使用if,因为存在下面这种情况,用if的结果是错误的。对于递减的元素,遇到一个大数时,需要把栈里的元素都依次弹出,来找到之前递减元素的对应Next Greater Element。Input:
[1,3,5,2,4]
[6,5,4,3,2,1,7]
Output:
[7,-1,-1,-1,-1]
Expected:
[7,7,7,7,7]
[1,3,5,2,4]
[6,5,4,3,2,1,7]
Output:
[7,-1,-1,-1,-1]
Expected:
[7,7,7,7,7]
阅读全文
0 0
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496.Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 496. Next Greater Element I
- 关于S7200的PPI地址无法更改
- [算法与数据结构]
- windows7 《寒江独钓》传统键盘过滤驱动学习
- 使用单例模式封装Intent(实现跳转与携带数据跳转,顺带发送广播)
- Java包装类 对象的比较器
- 496. Next Greater Element I
- Lecture 18
- 怎么解决java.lang.NoClassDefFoundError错误
- Android 6.0以后获取本地mac地址
- Unity自定义UI组件(六)日历、日期拾取器
- URL访问地址和Tomcat项目部署中不得不说的小秘密
- Unity3d生成Mesh
- Android Lint
- 更改dialog样式activity在屏幕中显示的位置