Next Greater Element II

转载 2018年04月16日 08:43:16

//这道题没有编出来,但是思想很好,首先倒序将前面的大数找出来,存入栈中,再进行第二次比较,得出最终结果

class Solution {

    public int[] nextGreaterElements(int[] nums) {
        int[] res = new int[nums.length];
        Stack<Integer> stack = new Stack<>();
        for (int i = 2 * nums.length - 1; i >= 0; --i) {
            while (!stack.empty() && nums[stack.peek()] <= nums[i % nums.length]) {
                stack.pop();
            }
            res[i % nums.length] = stack.empty() ? -1 : nums[stack.peek()];
            stack.push(i % nums.length);
        }
        return res;
    }
}

LeetCode 503. Next Greater Element II

503. Next Greater Element II Given a circular array (the next element of the last element is the fi...
  • liuchuo
  • liuchuo
  • 2017-02-10 01:55:25
  • 996

leetcode 503. Next Greater Element II

题意: 给定一个环形数组 nums, 对于每个元素e,找出e右边出现的第一个e',满足e‘ > e,若不存在e‘,令e’ 等于 1; Example: 给定 nums = {1, 2, 1},返回 {...
  • shiyang6017
  • shiyang6017
  • 2017-02-09 16:25:10
  • 593

[leetcode]503. Next Greater Element II

题目链接:https://leetcode.com/problems/next-greater-element-ii/ Given a circular array (the next eleme...
  • xiaocong1990
  • xiaocong1990
  • 2017-02-10 23:21:56
  • 424

【LeetCode】503. Next Greater Element II 解题报告(Python)

【LeetCode】503. Next Greater Element II 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2018-03-06 20:07:27
  • 13

503. Next Greater Element II(单调栈)

https://leetcode.com/problems/next-greater-element-ii/description/ 题目: 给一个循环数组,返回一个等长的数组,数组中的每一个元素...
  • tangyuanzong
  • tangyuanzong
  • 2018-02-06 16:53:35
  • 41

LeetCode 496. Next Greater Element I

496. Next Greater Element I You are given two arrays (without duplicates) nums1 and nums2 where num...
  • liuchuo
  • liuchuo
  • 2017-02-09 15:53:04
  • 1725

leetcode -- 496. Next Greater Element I 【栈】

题目 You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset ...
  • TheSnowBoy_2
  • TheSnowBoy_2
  • 2017-02-08 09:48:33
  • 1863

python--leetcode496. Next Greater Element I

You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of...
  • wenqiwenqi123
  • wenqiwenqi123
  • 2017-10-05 17:14:10
  • 459

496. Next Greater Element I \ 503. Next Greater Element II

Next Greater Element I 题目描述 代码实现496. Next Greater Element I题目描述ou are given two arrays (without dupl...
  • c602273091
  • c602273091
  • 2017-02-09 00:44:43
  • 976

Leetcode-556. Next Greater Element III

前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN...
  • mcf171
  • mcf171
  • 2017-04-09 11:21:21
  • 480
收藏助手
不良信息举报
您举报文章:Next Greater Element II
举报原因:
原因补充:

(最多只允许输入30个字)