Solution using Map<Integer, Integer>


  • 0
    L
    public class Solution {
        public int[] nextGreaterElement(int[] findNums, int[] nums) {
            int[] result = new int[findNums.length];
            Map<Integer, Integer> mapNums = new HashMap<Integer, Integer>();
            for (int i = 0; i < nums.length; i++) {
            	int currentNum = nums[i];
            	for (int j = i+1; j < nums.length; j++) {
            		if (nums[j] > currentNum) {
            			mapNums.put(currentNum, nums[j]);
            			break;
    				} else if (j == nums.length - 1) {
    					mapNums.put(currentNum, -1);
    				}
    			}
    		}
            if (nums.length > 0) {
            	mapNums.put(nums[nums.length-1], -1);
    		}
            
            for (int i = 0; i < findNums.length; i++) {
            	result[i] = mapNums.get(findNums[i]);
    		}
            return result;
        }
    }
    

Log in to reply
 

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