Java solution without HashMap or Stack


  • 1
    N
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int[] res = new int[nums1.length];
        for(int i = 0; i < nums1.length; i++)
        {
            boolean flag = false;
            int temp = Integer.MAX_VALUE;
            for(int j = 0; j < nums2.length; j++)
            {
                if(nums1[i] == nums2[j])
                    temp = nums2[j];
                if(temp < nums2[j])
                {
                    res[i] = nums2[j];
                    flag = true;
                    break;
                }
            }
            if(!flag)
                res[i] = -1;
        }
        return res;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.