Created
August 1, 2017 14:09
-
-
Save luojiyin1987/08a99829e0803a2e100995e04e85579e to your computer and use it in GitHub Desktop.
Next Greater Element I
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func nextGreaterElement(findNums []int, nums []int) []int { | |
| table := map[int]int{} | |
| stack := []int{} | |
| for _, n := range nums { | |
| size := len(stack) | |
| for size != 0 && stack[size-1] < n { | |
| table[stack[size-1]] = n | |
| stack = stack[:size-1] | |
| size-- | |
| } | |
| stack = append(stack, n) | |
| } | |
| result := []int{} | |
| for _,n := range findNums { | |
| m,flag := table[n] | |
| if flag == false { | |
| m = -1 | |
| } | |
| result= append(result, m) | |
| } | |
| return result | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
func nextGreaterElement(findNums []int, nums []int) []int {
if len(findNums) == 0 {
return []int{ }
}
}