美文网首页LeetCode solutions
496. Next Greater Element I[Easy

496. Next Greater Element I[Easy

作者: DrunkPian0 | 来源:发表于2017-06-15 23:30 被阅读2次

送分题。可以用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;
    }

相关文章

网友评论

    本文标题:496. Next Greater Element I[Easy

    本文链接:https://www.haomeiwen.com/subject/sbfpqxtx.html