送分题。可以用while的。有人用了Hashmap和stack,我就不看了。
public int[] nextGreaterElement(int[] findNums, int[] nums) {
int[] res = new int[findNums.length];
for (int i = 0; i < findNums.length; i++) {
int index = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[j] == findNums[i]) {
index = j;
break;
}
}
for (int k = index + 1; k < nums.length; k++) {
if (nums[k] > findNums[i]) {
res[i] = nums[k];
break;
}
}
if (res[i] == 0) {
res[i] = -1;
}
}
return res;
}
网友评论