[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 ...
随机推荐
- COMET技术具体实现 结合PHP和JQUERY
具体看代码,费话不说 PHP服务端 $mem = new RTMEM(); if(!$mem->conn()) exit('no mem server'); if(!$mem->getst ...
- phpcms 调用全站最新发布数据
phpcms模板标签没有调用全站最新发布的数据 所以参考phpcms本身自带的lists方法写了一个Countlists调用全站数据 /** * 全站最热 * @param $data */ publ ...
- eclipse IDE 扩展pydev
1. 安装PyDev. 运行Eclipse,打开菜单Help->Install New Software.在work with里输入网址:http://pydev.org/updates ,然后 ...
- 利用C#轻松创建不规则窗体
1.准备一个不规则的位图 可以使用任意一种你喜欢的作图工具,制作一个有形状的位图,背景使用一种其他的颜色.这个颜色在编程中用得着,所以最好使用一种容易记忆的颜色.如黄色,文件名为bk.bmp 2.创建 ...
- win7下设置 WiFi AP
开启windows 7的隐藏功能:虚拟WiFi和SoftAP(即虚拟无线AP),就可以让计算机变成无线路由器.实现共享上网. 1.以管理员身份运行命令提示符: “开始”---在搜索栏输入“cmd”-- ...
- android 自定义AlertDialog
xml: alter_dialog_two <?xml version="1.0" encoding="utf-8"?> <LinearLay ...
- #include <locale.h> #include <locale>
C C++ C 1 setlocale setlocale,本函数用来配置地域的信息,设置当前程序使用的本地化信息. #include <stdio.h> #include <std ...
- android setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds差别
手工设置文本与图片相对位置时.经常使用到例如以下方法: setCompoundDrawables(left, top, right, bottom) setCompoundDrawablesWithI ...
- URAL 1796. Amusement Park (math)
1796. Amusement Park Time limit: 1.0 second Memory limit: 64 MB On a sunny Sunday, a group of childr ...
- 关于cocos2dx 3.0升级崩溃报错(unable to load native library) 和(Fatal signal 11 (SIGSEGV) at 0x00000000)
近期一直在Windows平台开发cocos-2dx游戏,期间做了一次引擎升级,升级到了3.0正式版本号.Windows平台上表现非常正常,没有出现什么问题. 上周五准备公布一个安卓包,编译非常轻松的就 ...