32. Longest Valid Parentheses(最长括号匹配,hard)
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 ")()())"
, where the longest valid parentheses substring is "()()"
, which has length = 4.
code1:
()():返回2
class Solution:
def longestValidParentheses(self, s):
ans = 0
stack = []
for i,item in enumerate(s):
if item == '(':
stack.append(i)
else:
if(stack != []):
ans = max(ans,i-stack[-1]+1)
stack.pop()
return ans
code2:
()():返回4
不是很理解
class Solution:
def longestValidParentheses(self, s):
ans = 0
stack = []
last =-1
for i,item in enumerate(s):
if item == '(':
stack.append(i)
else:
if(stack == []):#已经匹配成功了
last = i
else:
stack.pop()
if stack==[]:
ans = max(ans,i-last)
else:
ans = max(ans,i-stack[-1])
return ans
32. Longest Valid Parentheses(最长括号匹配,hard)的更多相关文章
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [leetcode]32. Longest Valid Parentheses最长合法括号子串
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 32. Longest Valid Parentheses最长有效括号
参考: 1. https://leetcode.com/problems/longest-valid-parentheses/solution/ 2. https://blog.csdn.net/ac ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
随机推荐
- GSAP 官方文档(结贴)
好久没写GSAP的教程的(其实我也不懂哈哈),国内也没什么人用,不对动画要求特别高的话,其实也没必要用GSAP,现在工作上没用到这个东西,也懒得写了,所以有问题的话去找一下greensock的官方文档 ...
- RF内建的变量
${CURDIR} 提供当前测试文件存放的绝对路径.该变量是大小写敏感的.${TEMPDIR} 获取操作系统临时文件夹的绝对路径. 在UNIX系统是在/tmp, 在windows系统是在c:\Docu ...
- 自定义控件_StickyNavLaout
关注我一.View结构原理1.extends linearLayout 继承想要用的布局,首先完成布局的填充在 onFinishInflate 方法中 findViewById(); @Overrid ...
- Android无线测试之—UiAutomator UiWatcher API介绍一
UiWatcher类介绍与中断监听检查条件 一.UiWatcher类说明 1.Uiwatcher用于处理脚本执行过程中遇到非预想的步骤 2.UiWatcher使用场景 1)测试过程中来了一个电话 2) ...
- CxGrid如何实现导出Excel 功能
ExportGrid4ToEXCEL 这个老的版本用的,新的版本引用 cxGridExportLink 这个单元 uses Windows, Messages, SysUtils, Variant ...
- Eclipse启动Server报错:Could not publish to the server. java.lang.NullPointerException
转载自:http://m.blog.csdn.net/article/details?id=49862243 错误信息: publishing to tomcat v8.0 server at loc ...
- PAT 甲级 1026 Table Tennis(模拟)
1026. Table Tennis (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A table ...
- Servlet------>jsp自定义标签4(重复标签体)
4.控制标签体内容执行五次(tld中的配置,和jsp我就省略了,详细请看jsp自定义标签1)这里这个方法是继承了tag接口的实现类,这个实现类里不只实现了tag接口,还有tag接口的子接口,也就是It ...
- 二项分布。计算binomial(100,50,0.25)将会产生的递归调用次数(算法第四版1.1.27)
算法第四版35页问题1.1.27,估计用一下代码计算binomial(100,50,0.25)将会产生的递归调用次数: public static double binomial(int n,int ...
- Python中的Numpy
引用Numpy import numpy as np 生成随机数据 # 200支股票 stock_cnt = 200 # 504个交易日 view_days = 504 # 生成服从正态分布:均值期望 ...