题解 | #单调栈#

单调栈

http://www.nowcoder.com/practice/ae25fb47d34144a08a0f8ff67e8e7fb5

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param nums int一维数组 
# @return int二维数组
#
class Solution:
    def foundMonotoneStack(self , nums ):
        # write code here
        n = len(nums)
        stack = []

        res = [[-1, -1] for _ in range(n)]
        for i in range(len(nums)):
            while stack and nums[stack[-1]] > nums[i]:
                res[stack.pop()][1] = i 
            stack.append(i)

        stack = []
        for i in range(len(nums)-1, -1, -1):
            while stack and nums[stack[-1]] > nums[i]:                
                res[stack.pop()][0] = i
            stack.append(i)
        return res
全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务