博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode栈--3、longest-valid-parentheses(最长有效匹配括号长度)
阅读量:4613 次
发布时间:2019-06-09

本文共 770 字,大约阅读时间需要 2 分钟。

题目描述
 
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.
 
解题思路:这里我们还是借助栈来求解,需要定义个start变量来记录合法括号串的起始位置,我们遍历字符串,如果遇到左括号,则将当前下标压入栈,如果遇到右括号,如果当前栈为空,则将下一个坐标位置记录到start,如果栈不为空,则将栈顶元素取出,此时若栈为空,则更新最长长度,为之前的len和i - start + 1中的较大值,否则更新长度为len和i - 栈顶元素中的较大值
1 class Solution { 2 public: 3     int longestValidParentheses(string s) { 4         int len = 0; 5         int start = 0; 6         int n = s.size(); 7         stack
ss; 8 for(int i=0;i

 

转载于:https://www.cnblogs.com/qqky/p/6943783.html

你可能感兴趣的文章
Python垃圾回收机制详解
查看>>
jquery 编程的最佳实践
查看>>
MeetMe
查看>>
IP报文格式及各字段意义
查看>>
(转载)rabbitmq与springboot的安装与集成
查看>>
C2. Power Transmission (Hard Edition)(线段相交)
查看>>
STM32F0使用LL库实现SHT70通讯
查看>>
Atitit. Xss 漏洞的原理and应用xss木马
查看>>
MySQL源码 数据结构array
查看>>
(文件过多时)删除目录下全部文件
查看>>
T-SQL函数总结
查看>>
python 序列:列表
查看>>
web移动端
查看>>
pythonchallenge闯关 第13题
查看>>
个人介绍
查看>>
使用python动态特性时,让pycharm自动补全
查看>>
MySQL数据库免安装版配置
查看>>
你必知必会的SQL面试题
查看>>
html5 Canvas绘制时钟以及绘制运动的圆
查看>>
云推送注意(MSDN链接)
查看>>