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.

 

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.

思路:

https://blog.csdn.net/orangefly0214/article/details/96696883

和这个题是一样的解法,利用栈来实现。

实现:

import java.util.Arrays;
import java.util.Stack;
class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
       int[] ret=new int[nums1.length];
        int[] res=nextMax(nums2);
        for(int i=0;i<nums1.length;i++){
            int j=0;
            while(nums1[i]!=nums2[j]){
                j++;
            }
            ret[i]=res[j];                   
        }
        return ret;
        
    }
    private int[] nextMax(int[] nums){
	        int[] res=new int[nums.length];
	        Arrays.fill(res,-1);
	        Stack<Integer> s=new Stack<Integer>();
	        for(int i=0;i<nums.length;i++){
	            while(!s.isEmpty()&&nums[i]>nums[s.peek()]){
	                res[s.peek()]=nums[i];
	                s.pop();
	            }
	            s.push(i);
	            
	        }
	        return res;
	    }
}

另外,在leetcode上看到了另外一种更简单的思路:

https://leetcode.com/problems/next-greater-element-i/discuss/97595/Java-10-lines-linear-time-complexity-O(n)-with-explanation

思路2:

假如我们有一个递减的数组,后面跟了一个较大的元素,比如[5,4,3,2,1,6],那么后面这个较大的元素6将是前面所有元素的下一个较大值。

我们利用栈来存储一个递减的子序列,每当来一个新数x时,都与栈顶的元素进行比较,若大于栈顶,则将其弹出(并且我们会弹出栈内所有比x小的元素,且他们的下一个较大元素的值都是x);

比如[9,8,7,3,2,1,6]这个序列,栈内会保存[9,8,7,3,2,1],当6到来时,它大于栈顶的1,且大于2,3,所以我们会弹出1,2,3,且他们的next greater element 都是6

实现2:(推荐方法,利用HashMap存储)

public int[] nextGreaterElement(int[] nums1, int[] nums2) {
	 Stack<Integer> s=new Stack<>();
	 HashMap<Integer, Integer> map=new HashMap<>();//map的key存储元素,value存储元素的下一个较大值
	 //通过堆栈和HashMap辅助,遍历nums2,可获得nums2中每个元素的下一个较大值,没有的,则默认为-1
	 for(int i=0;i<nums2.length;i++){
		 while(!s.isEmpty()&&nums2[i]>s.peek()){
			 map.put(s.pop(), nums2[i]);
		 }
		 s.push(nums2[i]);
	 }
	 //遍历nums1,并从HashMap中获取每个的下一个较大值
	 int[] ret=new int[nums1.length];
	 for(int i=0;i<nums1.length;i++){
		 ret[i]=map.getOrDefault(nums1[i], -1);
		 
	 }
	 return ret; 
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值