496. Next Greater Element I[Easy]

送分题。可以用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;
    }
    原文作者:DrunkPian0
    原文地址: https://www.jianshu.com/p/02a499e49158
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞