Advertisement
jinhuang1102

32. Longest Valid Parentheses

Oct 22nd, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.48 KB | None | 0 0
  1. class Solution:
  2.     def longestValidParentheses(self, s):
  3.         """
  4.        :type s: str
  5.        :rtype: int
  6.        """
  7.         maxans = 0
  8.         st = []
  9.         st.append(-1)
  10.         for i in range(0, len(s)):
  11.             if s[i] == '(':
  12.                 st.append(i)
  13.             else:
  14.                 st.pop()
  15.                 if not st:
  16.                     st.append(i)
  17.                 else:
  18.                     maxans = max(maxans, i - st[-1])
  19.         return maxans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement