码迷,mamicode.com
首页 > 编程语言 > 详细

Longest Valid Parentheses @Leetcode -- Python

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

标签:style   blog   class   code   java   tar   

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

标签:style   blog   class   code   java   tar   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!