Merge pull request #713 from Spongecaptain/patch-9
Update 0503.下一个更大元素II.md
This commit is contained in:
commit
5530638f1c
|
|
@ -132,6 +132,26 @@ class Solution:
|
||||||
return dp
|
return dp
|
||||||
```
|
```
|
||||||
Go:
|
Go:
|
||||||
|
```go
|
||||||
|
func nextGreaterElements(nums []int) []int {
|
||||||
|
length := len(nums)
|
||||||
|
result := make([]int,length,length)
|
||||||
|
for i:=0;i<len(result);i++{
|
||||||
|
result[i] = -1
|
||||||
|
}
|
||||||
|
//单调递减,存储数组下标索引
|
||||||
|
stack := make([]int,0)
|
||||||
|
for i:=0;i<length*2;i++{
|
||||||
|
for len(stack)>0&&nums[i%length]>nums[stack[len(stack)-1]]{
|
||||||
|
index := stack[len(stack)-1]
|
||||||
|
stack = stack[:len(stack)-1] // pop
|
||||||
|
result[index] = nums[i%length]
|
||||||
|
}
|
||||||
|
stack = append(stack,i%length)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
JavaScript:
|
JavaScript:
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue