My 9 lines solution in Python


  • 0
    Q
    class Solution:
        # @param {string} s
        # @return {integer}
        def longestValidParentheses(self, s):
            m, stack, L = [0]*len(s), [], []
            for i in range(len(s)):
                if s[i] == '(':
                    stack.append(i)
                elif s[i] == ')' and stack:
                    j = stack.pop()
                    m[i], m[j] = 1, 1
            reduce(lambda x,y: x*y+y if not L.append(x*y+y) else 0, m, 0)
            return max(L) if s else 0

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.