# -*- 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. Android_CodeWiki_03

    1.发送不重复的通知(Notification) public static void sendNotification(Context context, String title, String m ...

  2. centos6.7配置git服务器

    1.yum install -y git 2.adduser git 3.cd /data/git 没有则创建该目录 git init --bare test.git;创建一个裸仓库,没有工作区,不需 ...

  3. Java数据结构习题_算法分析

    2.设T1(N)=O(f(N)),T2(N)=O(f(N)),则: T1(N)-T2(N)=o(f(N))           False,若1位2N,2为N T1(N)/T2(N)=O(1)     ...

  4. 我的android studio

  5. js 概念(构造函数)

    所有关于类.对象的语言里面,都有构造函数的概念,其实构造函数,就是在创建这个对象或者类的实例时候自动调用的函数,一般的语言都是new创建,那么new的参数就传递给构造函数.

  6. DELPHI 任务栏无EXE显示

    需要用到的一个函数: LONG SetWindowLong( HWND hWnd, int nIndex, LONG dwNewLong ); program Project; usesForms,  ...

  7. 推荐一些C#相关的网站、资源和书籍(转载)

    原文地址:http://blog.csdn.net/chinacsharper/article/details/17514923 一.网站 1.http://msdn.microsoft.com/zh ...

  8. AndroidUI 侧滑菜单 DrawerLayout的使用

    直接上代码: activity_main.xml: <android.support.v4.widget.DrawerLayout xmlns:android="http://sche ...

  9. @Transactional 注解说明

    先让我们看代码吧! 以下代码为在"Spring3事务管理--基于tx/aop命名空间的配置"基础上修改.首先修改applicationContext.xml如下: <pre ...

  10. mac隐藏或显示文件

    1,显示方法:在“终端” 输入命令 defaults write com.apple.finder AppleShowAllFiles TRUE killall Finder 重启Finder,系统隐 ...