4 lines solution with Java 8 Stream


  • 0
    S
    	public int[] nextGreaterElement(int[] findNums, int[] nums) {
    		return Arrays.stream(findNums).map(n -> {
    			int index = IntStream.range(0, nums.length).filter(i -> n == nums[i]).findFirst().getAsInt();
    			return IntStream.range(index + 1, nums.length).map(i -> nums[i]).filter(v -> v > n).findFirst().orElse(-1);
    		}).toArray();
    	}
    

Log in to reply
 

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