LeetCode-496. Next Greater Element I (java)

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:

  1. All elements in nums1 and nums2 are unique.
  2. The length of both nums1 and nums2 would not exceed 1000.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

题意
这道题题意是根据nums1的值,找到nums2相应位置的右边起,大于nums1本身的值

思路
根据我的思路有3步:
1. 遍历子集数组nums1
2. 找到nums1中的值在nums2中的位置
3. 从找到的位置开始,向右遍历,找到比他大的值

代码
根据这三步,我的代码如下,耗时18ms:
public class Solution {
    public int[] nextGreaterElement(int[] findNums, int[] nums) {
        int [] result = new int[findNums.length];
	int z=0;
	for(int i=0;i<findNums.length; i++){
		int index = SearchIndex(findNums[i],nums);
		//向右遍历 ,找到next greater
		for(int j=index;j<nums.length;j++){
			if(nums[j] > findNums[i]){
				result[z]= nums[j];
				break;
			}else{
				result[z] = -1;
			}
		}
		z++;
	}
	return result;
    }
    //用来找到位置
    public static int SearchIndex(int numElement,int[] nums){
	int index=0;
	for(int i=0;i<nums.length;i++){
		if(nums[i] == numElement){
			index = i;
		}
	}
	return index;
	}
}

还有一种耗时3ms的方法,代码如下:
public class Solution {
    public int[] nextGreaterElement(int[] findNums, int[] nums) {
        if (nums == null || nums.length == 0) {
            return new int[]{};
        }
        int Length = nums.length;
        int findLength = findNums.length;
        int[] Final = new int[findLength];
        int max = nums[0];
	    //找到nums中最大的值
        for (int i = 1; i < Length; i++) {
            if (nums[i] > max) {
                max = nums[i];
            }
        }
        int[] Indexer = new int[max + 1];
        for (int i = 0; i < Length; i++) {
	    //通过一个新的数组,将原数组的值作为索引,原数组的索引作为值,
        //这样可以很方便的找到指定数组元素的下标,只需要一次循环即可,而我写的findNums
	    //中有多少元素,就需要多少次循环,效率很低。 这里需要学习
            Indexer[nums[i]] = i;
        }
        boolean Found = false;
        int cur, curindex;
        for (int i = 0; i < findLength; i++) {
            Found = false;
            cur = findNums[i];
	        //找到需要遍历的索引值
            curindex = Indexer[cur] + 1;
	        //初始化为-1
            Final[i] = -1;
    	    //判断当前值是否是最大值,如果是最大值,就不用找了,而我写的没有此类判断,效率低下
            if (cur != max) {
	           	//如果已经找到大于cur的值,就不用再找了,用Found进行控制
                while (curindex < Length && Found != true) {
                    if (nums[curindex] > cur) {
                        Found = true;
                        Final[i] = nums[curindex];
                    }
                    curindex++;
                }
            }
        }
        return Final;
    }
}

通过引入新的数组,原数组的值作为索引,原数组的索引作为值,方便实现通过元素值找索引的需求,在 LeetCode-561 Array Partition I中也有此种体现,通过将数组值作为索引实现排序。


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值