update (0496.下一个更大元素I): 增加未精简Go版本

This commit is contained in:
dmzlingyin
2022-04-30 11:04:17 +08:00
parent 636550d44c
commit 49ec574821

View File

@ -244,6 +244,39 @@ class Solution:
```
Go
> 未精简版本
```go
func nextGreaterElement(nums1 []int, nums2 []int) []int {
res := make([]int, len(nums1))
for i := range res { res[i] = -1 }
m := make(map[int]int, len(nums1))
for k, v := range nums1 { m[v] = k }
stack := []int{0}
for i := 1; i < len(nums2); i++ {
top := stack[len(stack)-1]
if nums2[i] < nums2[top] {
stack = append(stack, i)
} else if nums2[i] == nums2[top] {
stack = append(stack, i)
} else {
for len(stack) != 0 && nums2[i] > nums2[top] {
if v, ok := m[nums2[top]]; ok {
res[v] = nums2[i]
}
stack = stack[:len(stack)-1]
if len(stack) != 0 {
top = stack[len(stack)-1]
}
}
stack = append(stack, i)
}
}
return res
}
```
> 精简版本
```go
func nextGreaterElement(nums1 []int, nums2 []int) []int {
res := make([]int, len(nums1))