题目来源


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. UVA 10325 - The Lottery(容斥)

    以前做过的一个题,忘记/gcd了,看来需要把以前的东西看一下啊. #include <cstdio> #include <cstring> #include <iostr ...

  2. 提示用户升级IE6浏览器的办法

    IE6一直饱受设计者们的诟病,互联网风云变幻十多年,唯一没变的就是这款在当初被微软内置在winxp系统而又火的不行的浏览器.而如今,在日新月异的网络环境下,IE6却让一个原本美观整洁的网页变得满目疮痍 ...

  3. thinkphp中M()和D()的理解

    在tp框架中基于MVC设计模式中的model文件夹下,处理数据时会创建和表相关的模型类文件.在控制器中需要使用时需要实例化模型类对象,写语句 1.$a = new GoodsModel(); 这是基于 ...

  4. 如何在64位windows7上同时使用32位和64位的Eclipse

    我用的是64位的windows7旗舰版,jdk1.7 64位机器上可以同时运行32位和64位的Eclipse,但是电脑中必须有相应的jdk.Eclipse虽然不需要安装,但是在启动时会检查系统中固定文 ...

  5. Powershell的远程管理

    powershell有强大的远程管理功能,但是现在遇到个问题,我们之前的客户端操作系统都是默认安装的,没做默认设置,请问如何通过gpo将所有和远程有关的设置都搞定啊?到底要设置哪些个选项?   我的环 ...

  6. Yii2 Format 如何使用

    $formatter = \Yii::$app->formatter; // output: January 1, 2014 echo $formatter->asDate('2014-0 ...

  7. apache日志文件太大的问题

    apache日志文件太大的问题 处理Apache日志的两种方法 rotatelogs 是 Apache 2.2 中自带的管道日志程序 rotatelogs [ -l ] logfile [ rotat ...

  8. Android shell 命令总结

    Package Manage(PM) pm list packages [FILTER] 查看已安装的应用包 -f 显示关联的apk文件 -s 只在系统应用中搜索Filter -3 只在第三方应用中搜 ...

  9. 【转载】wireshark:no interface can be used for capturing in this system with the current configuration

    转自:wireshark:no interface can be used for capturing in this system with the current configuration 通过 ...

  10. UITableview cell中多个按钮

    xib的 //不使用这种- (IBAction)button:(UIButton *)sender; //使用这种 @property (weak, nonatomic) IBOutlet UIBut ...