# -*- 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. 解决jquery zclip 插件点击无效的问题

    使用jquery zclip 用于页面复制文本内容. 首先引入js <script type="text/javascript" src="../js/jquery ...

  2. Composer加速

    在composer.json中添加{ "repositories": [ {"type": "composer", "url&qu ...

  3. Linux系统针对网卡中断的优化处理

    摘要: 中断: 当网卡接收到数据包后,会触发硬中断,通知CPU来收包.硬中断是一个CPU和网卡交互的过程.这其实会消耗CPU资源.特别是在使用速度极快的万兆网卡 之后,大量的网络交互使得CPU很大一部 ...

  4. eclipse 改包名

    转载自: http://www.2cto.com/kf/201304/206747.html 1.在项目上右键,选择android tools->rename application packa ...

  5. jQuery插入节点的方法

    注:摘自<锋利的jQuery(第二版)> append() 向每个匹配的元素内部追加内容:  HTML代码:<p>我想说:</p>jQuery代码:$(" ...

  6. 8.2.5: Spring3.0新增的@DependsOn和@Lazy

    @DependsOn用于强制初始化其他Bean.可以修饰Bean类或方法,使用该Annotation时可以指定一个字符串数组作为参数,每个数组元素对应于一个强制初始化的Bean. @DependsOn ...

  7. mysql zk切换整个过程

    <pre name="code" class="html">mysql master: test:/root/zk# cat zk.pl use Z ...

  8. javascript 汉字拼音排序

    定义和用法 用本地特定的顺序来比较两个字符串. 语法 stringObject.localeCompare(target) 参数 描述 target 要以本地特定的顺序与 stringObject 进 ...

  9. 网易云课堂_C语言程序设计进阶_第三周:结构:结构、类型定义、联合

    3.1 枚举 3.2 结构 3.3 类型定义 3.1 枚举 枚举是一种用户定义的数据类型,它用关键字enum以如下语法来表明: enum 枚举类型名字{名字0,...,名字n}; 枚举类型名字通常并不 ...

  10. 2014百度之星第四题Labyrinth(DP)

    Labyrinth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...