首页 > 编程语言 > 详细

Longest Valid Parentheses @Leetcode -- Python

时间:2014-05-09 00:11:57      阅读:477      评论:0      收藏:0      [点我收藏+]

http://oj.leetcode.com/problems/longest-valid-parentheses/

bubuko.com,布布扣
 1 class Solution:
 2     # @param s, a string
 3     # @return an integer
 4     def longestValidParentheses(self, s):
 5         if s == ‘‘ or s == ( or s == ):
 6             return 0
 7         stack = [(-1, ))]
 8         maxLen = 0
 9         for i in xrange(len(s)):
10             if s[i] == ) and stack[-1][1] == (:
11                 stack.pop()
12                 maxLen = max(maxLen, i - stack[-1][0])
13             else:
14                 stack.append((i, s[i]))
15         return maxLen
16 # test
17 s = Solution()
18 print s.longestValidParentheses("()(()")
19 print s.longestValidParentheses((()())
20 print s.longestValidParentheses()()()))
bubuko.com,布布扣

 

Longest Valid Parentheses @Leetcode -- Python,布布扣,bubuko.com

Longest Valid Parentheses @Leetcode -- Python

原文:http://www.cnblogs.com/sumnous/p/3716432.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!