Longest Valid Parentheses @Leetcode -- Python

http://oj.leetcode.com/problems/longest-valid-parentheses/


 1 class Solution:
2 # @param s, a string
3 # @return an integer
4 def longestValidParentheses(self, s):
5 if s == ‘‘ or s == ‘(‘ or s == ‘)‘:
6 return 0
7 stack = [(-1, ‘)‘)]
8 maxLen = 0
9 for i in xrange(len(s)):
10 if s[i] == ‘)‘ and stack[-1][1] == ‘(‘:
11 stack.pop()
12 maxLen = max(maxLen, i - stack[-1][0])
13 else:
14 stack.append((i, s[i]))
15 return maxLen
16 # test
17 s = Solution()
18 print s.longestValidParentheses("()(()")
19 print s.longestValidParentheses(‘(()()‘)
20 print s.longestValidParentheses(‘)()())‘)

时间: 2024-10-05 04:54:55

Longest Valid Parentheses @Leetcode -- Python的相关文章

Longest Valid Parentheses leetcode java

题目: 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

Longest Valid Parentheses Leetcode 32 一种奇特的解法

题目: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 i

Longest Valid Parentheses - LeetCode

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 &

[Leetcode][Python]32: Longest Valid Parentheses

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 32: Longest Valid Parentheseshttps://oj.leetcode.com/problems/longest-valid-parentheses/ Given a string containing just the characters '(' and ')',find the length of the longest valid (well-fo

LeetCode 32 Longest Valid Parentheses (C,C++,Java,Python)

Problem: 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 exa

LeetCode: Longest Valid Parentheses [031]

0.下载安装Opencv,当前版本为249. 1.下载Python,当前OPencv版本为249,不过其支持的最新版本的Python为2.7,所以可以下载276版本. 2.下载numpy,开始我使用了1.6,没有通过,错误如图.下载了最新的1.8.1版本. 3.将Opencv安装目录下opencv\build\python\2.7\x86中的cv2.pyd复制到python安装目录Lib\site-packages下. 4.找到opencv源文件内的draw.py运行. LeetCode: Lo

[LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Longest Valid Parentheses (Hard) 链接: 题目:https://oj.leetcode.com/problems/longest-valid-parentheses/ 代码(github):https://github.com/illuz/leetcode 题意: 问一个字

Leetcode 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 &

[LeetCode]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 i