解法一:
public class Solution { public int[] nextGreaterElement(int[] findNums, int[] nums) { int[] res = new int[findNums.length]; //因为数组findNums是数组nums的子集,所以数组findNums的每个元素都能在数组nums中找到,只是判断找的位置 //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. for(int i = 0; i < findNums.length; i++){ //先找到findNums[i]在nums中的位置,然后再去nums中后面找是否存在大于findNums[i]的数 int index = -1; for(int k = 0; k < nums.length; k++){ if(nums[k] == findNums[i]){ index = k; break; } } //去nums中后面找是否存在大于findNums[i]的数 boolean isFind = false; //System.out.println(index); for(int j = index + 1; j < nums.length; j++){ if(nums[j] > findNums[i]){ isFind = true; res[i] = nums[j]; break; } } //假如nums中不存在满足条件的 if(!isFind){ res[i] = -1; } } return res; } }