496. Next Greater Element I(C语言版本)

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.


解释:题目的意思是有两个数组,nums2 为原始数组,数组nums1是nums2元素的子集,要求得到nums1中的数据,在nums2中的位置的右侧比其本身

  的值要大的值 的 第一个位置。如果没有的话,值为-1


解题思路:

1 应为nums2中的整数是不相同的,所以可以定义一个hashnum数组,数组中的索引为nums1中存储的数,具体值为右侧比它大的值(最近的)

   举个例子例如: [0,7,8,9]

nums1[1] = 7; 离它最近的比它大的值为nums1[2] = 8 ,所以hashnum[nums1[1]] = nums1[2] 

/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* nextGreaterElement(int* findNums, int findNumsSize, int* nums, int numsSize, int* returnSize) {
    int* returnArray = NULL;
    int* numsHash = NULL;

    *returnSize = findNumsSize;

    if(findNums != NULL && findNumsSize != 0 &&
       nums != NULL && numsSize != 0) {
        /* initalize the returnArray */
        returnArray = malloc((*returnSize) * sizeof(int));
        
        /* find the max element in the nums */
        int max = 0;
        for(int i = 0; i < numsSize; i++) {
            if(max < nums[i]) {
                max = nums[i];
            }
        }

        /* initialize the numsHash */
        numsHash = malloc((max+1) * sizeof(int));
    
        /* hash the nums' element to numsHash, the key is nums's element, */
        /* the velue is the next greater element, or -1 if does not exist */
        /* nums[numsSize-1] is the last element of nums, don't have next, */
        /* so set it to -1 directly */
        for(int i = 0; i < numsSize-1; i++) {
            numsHash[nums[i]] = -1;
            for(int j = i+1; j < numsSize; j++) {
                if(nums[j] > nums[i]) {
                    numsHash[nums[i]] = nums[j];
                    break;
                }
            }
        }
        numsHash[nums[numsSize-1]] = -1;

        /* find the next greater element */
        for(int i = 0; i < findNumsSize; i++) {
            returnArray[i] = numsHash[findNums[i]];
        }
    }

    return returnArray;
}

改进方法:

因为这个题是在stack 这个tag下,所以很自然的就想用stack去实现,但是想了很久都没有弄清要如何实现,看了题目给的solution,豁然开朗。

具体的流程,除了和上面的方法一样,需要建立 hashnum数组,还要建立一个stack,stack里面的数都是从大到小排列,依次与最新的整数比较,如果栈顶的元素小于当前值,在hashname数组相应位置插入相应的值,出栈,继续判断栈顶的值与当前值的大小,直到栈空,或者栈顶元素大于当前值。将当前值入栈。


  public int[] nextGreaterElement(int[] findNums, int[] nums) {
        Map<Integer, Integer> map = new HashMap<>(); // map from x to next greater element of x
        Stack<Integer> stack = new Stack<>();
        for (int num : nums) {
            while (!stack.isEmpty() && stack.peek() < num)
                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;
    }





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值