# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheses
https://oj.leetcode.com/problems/longest-valid-parentheses/ Given a string containing just the characters '(' and ')',
find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4. ===Comments by Dabay===
用一个stack来记录左括号'('的位置;
start来记录这个左括号之前自封闭的起始位置。也就是说,实际上匹配到这个左括号的时候,计算右括号到start的长度。
例如:字符串“()()”,当遍历到第二个“(”的时候,实际上入栈的位置是0而不是2。 当遇到右括号'('的时候,
把i和start中小的那个数入栈;同时,start更新指向i的下一个位置。
当遇到左括号')'的时候,
如果stack不为空,
出栈,计算是否需要更新max_so_far
同时,更新start为出栈的数字
如果stack为空,
start指向i的下一个位置
''' class Solution:
# @param s, a string
# @return an integer
def longestValidParentheses(self, s):
start = 0
max_so_far = 0
stack = []
for i in xrange(len(s)):
if s[i] == "(":
stack.append(min(i, start))
start = i + 1
else:
if len(stack)>=1:
start = last = stack.pop()
max_so_far = max(i-last+1, max_so_far)
else:
start = i + 1
return max_so_far def main():
s = Solution()
print s.longestValidParentheses("()()") if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python]32: Longest Valid Parentheses的更多相关文章

  1. 【一天一道LeetCode】#32. Longest Valid Parentheses

    一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...

  2. leetcode problem 32 -- Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  3. 【LeetCode】32. Longest Valid Parentheses (2 solutions)

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  4. 【LeetCode】32. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  5. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  6. 刷题32. Longest Valid Parentheses

    一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...

  7. [LeetCode] 32. Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  8. leetcode 32. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. Java [leetcode 32]Longest Valid Parentheses

    题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...

随机推荐

  1. js/jquery获取当前页面URL地址并判断URL字符串中是否包含某个具体值

    js/jquery获取当前页面URL地址并判断URL字符串中是否包含某个具体值本文介绍jquery/js获取当前页面url地址的方法,在jquery与js中获取当前页面url方法是一样的,因为jque ...

  2. php获取当前日期-7天

    // 将目前的时间戳值放入一数组内$strdate = '2014-02-03';$desDate = strtotime($strdate);//var_dump($desDate); $times ...

  3. OpenGL ES 三种类型 uniform attribute varying

    1.uniform变量 uniform变量是外部application程序传递给(vertex和fragment)shader的变量.因此它是application通过函数glUniform**()函 ...

  4. poj2864

    #include<iostream> #include<cstdio> #include<cmath> using namespace std; int main ...

  5. hdu 2604 Queuing(矩阵快速幂乘法)

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

  6. jquery实现文字选择器

     $( "div:contains('John')" ).css( "text-decoration", "underline" ); 

  7. 在mangento后台调用wysiwyg编辑器

    在mangento后台调用操蛋的wysiwyg编辑器: 1.在头部加载TincyMCE protected function _prepareLayout() {     parent::_prepa ...

  8. [连载]JavaScript讲义(02)--- JavaScript核心编程

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamFja2ZydWVk/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  9. UART串口协议基础1

    Louis kaly.liu@163.com 串口协议基础 1 串口概述 串口由收发器组成.发送器是通过TxD引脚发送串行数据,接收器是通过RxD引脚接收串行数据. 发送器和接收器都利用了一个移位寄存 ...

  10. Moss 几个编程技巧

    1.提升权限执行的代码 SPSecurity.RunWithElevatedPrivileges(delegate() { // 需要提升权限执行的代码 }); 应用场景:当前用户可能没有权限执行的操 ...