LeetCode-496 Next Greater Element I Solution(with Java)

 

1. Description:

Notes:

2. Examples:

3.Solutions:

 

/**
 * Created by sheepcore on 2018-12-24
 */
class Solution {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int l1 = nums1.length, l2 = nums2.length;
        int[] res = new int[l1];
        for(int i  = 0; i < l1; i++){
            for(int j = 0; j < l2; j++){
                if(nums2[j] != nums1[i])  //locate the number in nums2
                    continue;
                else{
                    int k;
                    for(k = j + 1; k < l2; k++){
                        if(nums2[k] > nums1[i]) { //find the next greater one
                            res[i] = nums2[k];
                            break;
                        }
                    }
                    if(k == l2) //don't find the next greater one
                        res[i] = -1;
                    break;

                }
            }
        }
        return res;
    }
}

 

 

posted @ 2020-03-02 17:00  SheepCore  阅读(120)  评论(0编辑  收藏  举报