代码如下
func nextGreaterElements(nums []int) []int {
length := len(nums)
res := make([]int,length)
for i := 0 ; i < length ; i++ {
res[i] = -1
}
stack := []int{}
for i := 0 ; i < 2*length ; i++ {
for len(stack)>0 && nums[i%length] > nums[stack[len(stack)-1]] { 和上一题基本一样就是这里递增i的方式是取模
top := stack[len(stack)-1]
stack = stack[:len(stack)-1]
res[top] = nums[i%length]
}
stack = append(stack,i%length)
}
return res
}