# 503.下一个更大元素II

[https://leetcode-cn.com/problems/next-greater-element-ii/](https://leetcode-cn.com/problems/next-greater-element-ii/)

技巧一：**对于这种需求，常用套路就是将数组长度翻倍**：

![](https://3747312504-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LXItOiQMDgh0S65YZXd%2Fuploads%2Fgit-blob-9ac915a5cd4b6bd1d5509102c0eccdcc3b7afc6c%2F503_1.jpeg?alt=media)

然后在翻倍数组上，按496. 下一个更大元素 I的解法三，套用单调栈模板即可

技巧二：实际上不用真的把数组翻倍，只需取模运算，”假装“该数组长度翻倍

```python
class Solution:
    def nextGreaterElements(self, nums) :
        n = len(nums)
        res = [-1]*n
        stack = []
        # ”假装“该数组长度翻倍，然后用取模映射到原数组
        for i in range(n*2-1, -1, -1):
            while stack and stack[-1] <= nums[i%n]:
                stack.pop()
            res[i%n] = stack[-1] if stack else -1
            stack.append(nums[i%n])
        return res
```
