题目来源


https://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.


题意分析


Input:str

Output:length

Conditions:返回最长有效子串

       有效:符合括号匹配规律


题目思路


用一个list存储左括号‘(’的位置(index),用一个变量last(起始值为0)存储某一个有效字串的开始(last不断更新),用maxlength存储全局最大长度,如果遇到右括号‘)’,

  1. 如果此时list为空,表示没有左括号与之匹配,此时length = i - last;同时更新last = i + 1(表示last之前的串没用了),将list置为空;
  2. 如果list不为空,说明可以消除,则list.pop(),pop完之后,如果list为空,说明在last之后的串都符合,length = i + 1 - last;如果list不为空,则说明list最后一个位置之后的串都符合,length = i - L[-1]
  3. 根据length与maxlength大小持续更新maxlength

AC代码(Python)


 class Solution(object):
def longestValidParentheses(self, s):
"""
:type s: str
:rtype: int
"""
maxlength = 0
L = []
last = 0
if len(s) == 0:
return 0
for i in range(len(s)):
#print(len(s),len(L))
if s[i] == '(':
L.append(i)
elif s[i] == ')':
if len(L) == 0:
length = i - last
L = []
last = i + 1
if length > maxlength:
maxlength = length else:
L.pop()
if len(L) == 0:
length = i + 1 - last
else:
length = i - L[-1]
if length > maxlength:
maxlength = length return maxlength

[LeetCode]题解(python):032-Longest Valid Parentheses的更多相关文章

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

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

  2. Java for LeetCode 032 Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. [Leetcode][Python]32: Longest Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...

  4. LeetCode 032 Longest Valid Parentheses

    题目描述:Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the l ...

  5. LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]

    题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...

  6. leetcode第31题--Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  7. leetcode解题报告 32. Longest Valid Parentheses 用stack的解法

    第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...

  8. LeetCode (32) Longest Valid Parentheses

    题目 Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...

  9. 032 Longest Valid Parentheses 最长有效括号

    给一个只包含 '(' 和 ')' 的字符串,找出最长的有效(正确关闭)括号子串的长度.对于 "(()",最长有效括号子串为 "()" ,它的长度是 2.另一个例 ...

  10. leetcode解题报告 32. Longest Valid Parentheses 动态规划DP解

    dp[i]表示以s[i]结尾的完全匹配的最大字符串的长度. dp[] = ; ; 开始递推 s[i] = ')' 的情况 先想到了两种情况: 1.s[i-1] = '(' 相邻匹配 这种情况下,dp ...

随机推荐

  1. myeclipse下安装svn

    在网上查了一下,安装的方法有几种,这里给大家推荐一种快速安装的方法. //第一步 : 下载 site-1.6.5.zip //===================================== ...

  2. BZOJ3776 : 警察局

    怎么3776又换题目了…换题目了…题目了…目了…了… SCC缩点后只有入度或者出度为0的点必须要放警察局 假设一共有t-1个入度或者出度为0的SCC q[1]-q[t-1]表示这些SCC中点的个数 q ...

  3. BZOJ3738 : [Ontak2013]Kapitał

    $C_{N+M}^N=\frac{(N+M)!}{N!M!}$ 考虑求出$ans\bmod 10^9$的值 $10^9=2^9\times5^9$ 以$2^9$为例,先预处理出$1$..$2^9$中不 ...

  4. 原来还有这样的记词方法_Java版记不规则动词_博主推荐

    昨天在看一本英语书的不规则动词的时候,突然产生的灵感:就是想把这样记单词简单方式,用程序代码实现,然后,使用户可以与之进行交互 这样,在用户背不规则动词的时候就会轻松把它给记住.基于这一点,于是我就思 ...

  5. DWR入门教程

    DWR(Direct Web Remoting)是一个WEB远程调用框架.利用这个框架可以让AJAX开发变得很简单.利用DWR可以在客户端利用JavaScript直接调用服务端的Java方法并返回值给 ...

  6. org.apache.hadoop.hdfs.server.namenode.SafeModeException: Cannot create directory /user/hive/warehouse/page_view. Name node is in safe mode

    FAILED: Error in metadata: MetaException(message:Got exception: org.apache.hadoop.ipc.RemoteExceptio ...

  7. jsoncpp代码实例

    最近开始使用 jsoncpp,以前一直在使用cJSON,但是使用cJSON的时候经常会忘记free掉json的内存,结果造成了内存泄露,程序跑着跑着就崩溃了.所以最近把json转移到了jsoncpp上 ...

  8. .net 后台设置meta的属性(keywords,description)

    首先在前台aspx文件中的head标记添加runat="server"的属性. 之后后台如下编写: protected void Page_Load(object sender, ...

  9. 使用LTT升级HP磁带机的固件程序

    下载后将软件包解压 解压后,进入该文件夹可以看到固件程序     将所有固件程序拷贝至LTT软件安装目录下的firmware文件夹中 C:\Program Files\HP StorageWorks ...

  10. JSP简单访问数据库

    Java代码 public class DBHelper { private String driverName; private String url; private String user; p ...