32. Longest Valid Parentheses leetcode python new season 2016
2016-01-18 14:35
603 查看
Given a string containing just the characters
find the length of the longest valid (well-formed) parentheses substring.
For
which has length = 2.
Another example is
which has length = 4.
class Solution(object):
def longestValidParentheses(self, s):
"""
:type s: str
:rtype: int
"""
s = ')' + s
stack, result = [], 0
for index in range(len(s)):
element = s[index]
if element == ')' and stack and stack[-1][1] == '(':
stack.pop()
result = max(result, index - stack[-1][0])
else:
stack.append((index, s[index]))
return result
'('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.
class Solution(object):
def longestValidParentheses(self, s):
"""
:type s: str
:rtype: int
"""
s = ')' + s
stack, result = [], 0
for index in range(len(s)):
element = s[index]
if element == ')' and stack and stack[-1][1] == '(':
stack.pop()
result = max(result, index - stack[-1][0])
else:
stack.append((index, s[index]))
return result
相关文章推荐
- 安装numpy只需一步简单的方法
- 利用python将alfw转换成bing标注格式
- 31. Next Permutation leetcode Python 2016 new Season
- python(1) - 安装篇
- python学习----------正则表达式
- python 和 sqlmap的安装使用
- Python 4.3 继承和多态
- python中基于descriptor的一些概念
- ubuntu 文件重命名
- 用python将SQL格式文件改成自己想要的格式
- python 深浅拷贝
- Python算法题----逆序列表
- Python Tkinter Grid布局管理器详解
- 如何让你的Python代码更加pythonic ?
- Python--day1
- python中的 \r以及基于此实现简单的倒计时小程序
- 笨方法学python(ex1-17)--%,input,解包,读取,写文件
- 《Python for kids》学习笔记(二)
- 用python做自动化测试--对Java代码做单元测试 (1)
- 运行python