[LeetCode]题解(python):032-Longest Valid Parentheses
题目来源
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存储全局最大长度,如果遇到右括号‘)’,
- 如果此时list为空,表示没有左括号与之匹配,此时length = i - last;同时更新last = i + 1(表示last之前的串没用了),将list置为空;
- 如果list不为空,说明可以消除,则list.pop(),pop完之后,如果list为空,说明在last之后的串都符合,length = i + 1 - last;如果list不为空,则说明list最后一个位置之后的串都符合,length = i - L[-1]
- 根据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的更多相关文章
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- LeetCode 032 Longest Valid Parentheses
题目描述:Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the l ...
- LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- leetcode第31题--Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- leetcode解题报告 32. Longest Valid Parentheses 用stack的解法
第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...
- LeetCode (32) Longest Valid Parentheses
题目 Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- 032 Longest Valid Parentheses 最长有效括号
给一个只包含 '(' 和 ')' 的字符串,找出最长的有效(正确关闭)括号子串的长度.对于 "(()",最长有效括号子串为 "()" ,它的长度是 2.另一个例 ...
- leetcode解题报告 32. Longest Valid Parentheses 动态规划DP解
dp[i]表示以s[i]结尾的完全匹配的最大字符串的长度. dp[] = ; ; 开始递推 s[i] = ')' 的情况 先想到了两种情况: 1.s[i-1] = '(' 相邻匹配 这种情况下,dp ...
随机推荐
- object-c cocos2d-x 写程序时注意调试的技巧
(1)写程序时最好在类的init函数中显示类名,表明现在在执行哪个类,样例代码 CCLOG(@"cocos2d: Using Director Type:%@", [self cl ...
- Google Code Jam 2010 Round 1A Problem A. Rotate
https://code.google.com/codejam/contest/544101/dashboard#s=p0 Problem In the exciting game of Jo ...
- FlexSlider插件的详细设置参数
FlexSlider是一个非常出色的jQuery滑动切换插件,它支持所有主流浏览器,并有淡入淡出效果.适合所有初级和高级网页设计师使用.不过很多人都只是使用默认的参数,今天来说说具体的参数来给大家看看 ...
- JavaScript事件大全3
//无模式的提示框 //屏蔽按键 <html> <head> <meta http-equiv="Content-Type" content=& ...
- Qt Creator Shortcuts 快捷键大全
编号 快捷键 功能 1 Esc 切换到代码编辑状态 2 F1 查看帮助(选中某一类或函数,按下F1,出现帮助文档) 3 F2 在光标选中对象的声明和定义之间切换(和Ctrl+鼠标左键一样的效果,选中某 ...
- thinkphp模型没继承model报的错
Call to undefined method RoleModel::query() 错误位置 FILE: H:\www\tpsmarty\shop\Lib\Model\RoleModel.clas ...
- hello world 驱动程序编写
操作系统课程设计选题 驱动程序的编写和安装. 经过一天多的努力,终于把我的第一个驱动程序模块成功编写并实现插入内核和移除,在这里把过程记录下来方便以后查看,也给其他为之困扰的朋友一个建议. 环境: ...
- 对于默认 Windows NT 安装的 SID 值
https://support.microsoft.com/en-us/kb/163846/zh-cn
- 【新产品发布】【iM_TFTRGB 液晶驱动模块】
============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...
- NBUT 1525 Cow Xor(01字典树+前缀思想)
[1525] Cow Xor 时间限制: 2000 ms 内存限制: 65535 K 问题描述 农民约翰在喂奶牛的时候被另一个问题卡住了.他的所有N(1 <= N <= 100,000)个 ...