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 ...
随机推荐
- 关于Bootstrap的理解
Web开发领域存在大量的反复劳动.以创建一个菜单为例,不同的人或是同一个人在不同的时期去构建一个菜单.他创建出来的菜单格式都会存在差异:随着构件的菜单越来越多,我们会发现假设将构建菜单这件事形成一个框 ...
- 【BZOJ4726】[POI2017]Sabota? 树形DP
[BZOJ4726][POI2017]Sabota? Description 某个公司有n个人, 上下级关系构成了一个有根树.其中有个人是叛徒(这个人不知道是谁).对于一个人, 如果他 下属(直接或者 ...
- [SharePoint 2010] Visual Studio 2010內撰寫視覺化WebPart超簡單
新一代的Visual Studio 2010對於SharePoint 2010的專案撰寫,有非常另人讚賞的改進. 以往寫一個WebPart要搞好多雜七雜八的步驟,也要硬寫HTML輸出,當然有人說可以寫 ...
- 加载CDN加速服务地址
Jquery是个非常流行的JS前端框架,在很多网站都能看到它的身影.很多网站都喜欢采用一些Jquery CDN加速服务,这样网站加载jquery会更快.之前火端网络的一些网站都是使用Google的jq ...
- Vue-cli 安装使用和理解
Vue 的 官方文档 提到 点开这个链接,跟着文档一步步直到: $ npm install -g vue-cli $ vue init webpack my-project $ cd my-proje ...
- HDFS编程
HDFS编程主要API Hadoop类 功能 org.apache.hadoop.fs.FileSystem 一个通用文件系统的抽象基类,可以被分布式文件系统继承.所有的可能使用Hadoop文件系统的 ...
- python--sha256
import hmacimport hashlib def get_hmacsha256(key, message): key_bytes = bytes(key, 'utf-8') message_ ...
- 每日集成CruiseControl.NET + SVN + Msbuild + NAnt
CruiseControl.NET-1.8.4.0-Setup.exe 是服务器,安装时可以选择生成windows service以便开启,建议测试时不用windows se ...
- swift中类与结构体
写了12个Person来复习,不过完成同样的代码需要敲键盘的次数相比OC确实少了很多,这很多应该归功于Swift中不写分号,以及少了OC中的中括号. 一.类与结构体 两者在Swift中差不了多少了 类 ...
- Rete_algorithm
https://en.wikipedia.org/wiki/Rete_algorithm https://en.wikipedia.org/wiki/Rete_algorithm The Rete a ...