[Leetcode][Python]32: Longest Valid Parentheses
# -*- 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的更多相关文章
- 【一天一道LeetCode】#32. Longest Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...
- leetcode problem 32 -- Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【LeetCode】32. Longest Valid Parentheses (2 solutions)
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【LeetCode】32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
随机推荐
- 如何解决jquery库的冲突问题
多个库之间的冲突 当一个项目中引入多个第三方库的时候,由于没有命名空间的约束(命名空间就好比同一个目录下的文件夹一样,名字相同就会产生冲突),库与库之间发生冲突在所难免. 那么,既然有冲突的问题,为什 ...
- 处理json中的异常字符
在很多场景中需要通过json传递数据,如果json中包含英文的",""'"之类的字符,会导致json解析失败 可以用一些在线的json格式检查网站检查是否含有异 ...
- AIX 命令大全
http://www.ahinc.com/aix/general.htm http://web.mit.edu/javadev/packages/Acme/ http://jparsec.codeha ...
- JD-GUI on Ubuntu 13.04 64-bit
Java Decompiler (jd-gui) is a cute little tool I like using when working in Java. Unfortunately it o ...
- Aix 光盘软件包安装
1, mount -rv cdrfs /dev/cd0 /mnt (Aix 光盘挂载)2,fuser -kxcu /cdrom && unmount /mnt && u ...
- Struts2之自定义局部类型转换器、全局类型转换器
Struts2自定义类型转换器分为局部类型转换器和全局类型转换器 (1)局部类型转换器 如果页面传来一个参数reg.action?birthday=2010-11-12到后台action,然后属性用 ...
- 【转】C#自定义控件:WinForm将其它应用程序窗体嵌入自己内部
PS:文末的附件已更新,这次我放到博客园里面了,不会弹出广告,放心下载,O(∩_∩)O谢谢! 这是最近在做的一个项目中提到的需求,把一个现有的窗体应用程序界面嵌入到自己开发的窗体中来,看起来就像自己开 ...
- C# 如何利用反射来加载程序集,并调用程序集中有关类的方法【转】
假设在C盘根目录下有个Dog的Dll程序集文件,该程序集文件中包含类Dog 该类中有个狗叫几声的方法,如何通过反射来加载这个C:\Dog.dll,并且调用Dog类里面的Sound方法呢: public ...
- Sass介绍及入门教程
Sass是什么? Sass是"Syntactically Awesome StyleSheets"的简称.那么他是什么?其实没有必要太过于纠结,只要知道他是“CSS预处理器”中的一 ...
- Mina入门:mina版之HelloWorld
一,前言: 在完成上篇文章<Mina入门:Java NIO框架Mina.Netty.Grizzly简介与对比>之后,我们现在可以正式切入Mina入门学习了. 二,搭建项目结构与解决项目依赖 ...