Leetcode 栈 Longest Valid Parentheses

Longest Valid Parentheses

Total Accepted: 14818 Total
Submissions: 75749My Submissions

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.

题意:给定一个包含括号 ‘(‘,‘)‘的字符串,返回最长匹配的括号数目

思路1:栈

用一个变量_max表示当前最长匹配的括号数

不存右括号,只存左括号的index。扫描一遍字符串,

如果当前的字符是‘(‘,则压栈

如果当前的字符是‘)‘,则

1.若栈空,则当前的‘)‘没有一个‘(‘与它匹配,它可以作用它左右两个匹配的括号串的分割点,用一个变量 last 记录下它的坐标。

last的初始值是-1,当遇到新的分割点时,我们更新 last,并可以得到两个 last之间的匹配括号的长度

2.若栈非空,则当前的‘)‘与栈顶的‘(‘匹配

将栈顶元素出栈

如果此时栈为空,则找到了一组匹配括号串,更新 _max = max(_max, last - i)

如果此时栈不空,则栈顶的‘(‘还没找到它的‘)‘,因为栈顶元素有可能找不到它的‘)‘,因此,此时要更新 _max = max(_max, i - stack.top())

复杂度:时间O(n),空间O(n)

int longestValidParentheses(string s){
	int _max = 0, last = -1;
	stack<int> left;
	for(int i = 0;  i < s.size(); ++i){
		if(s[i] == '(') left.push(i);
		else{
			if(left.empty()) last = i;
			else{
				left.pop();
				if(left.empty()){
					_max = max(_max, i - last);
				}else{
					_max = max(_max, i - left.top());
				}
			}
		}
	}
	return _max;
}

时间: 2024-08-07 12:32:27

Leetcode 栈 Longest Valid Parentheses的相关文章

leetcode 之 Longest Valid Parentheses

leetcode中和括号匹配相关的问题共有三个,分别是: Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are a

[LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Longest Valid Parentheses (Hard) 链接: 题目:https://oj.leetcode.com/problems/longest-valid-parentheses/ 代码(github):https://github.com/illuz/leetcode 题意: 问一个字

LeetCode 之 Longest Valid Parentheses(栈)

[问题描写叙述] 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 exa

[LeetCode][JavaScript]Longest Valid Parentheses

https://leetcode.com/problems/longest-valid-parentheses/ Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid

Java for LeetCode 032 Longest Valid Parentheses

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 &

LeetCode - 32. Longest Valid Parentheses

32. Longest Valid Parentheses Problem's Link ---------------------------------------------------------------------------- Mean: 给定一个由'('和')'组成的字符串,求最长连续匹配子串长度. analyse: 定义一个stack<pair<char,int>>类型的stack. 遇到'('进栈; 遇到')'需要分两种情况讨论: 栈顶元素为'(',正好匹配,

Java [leetcode 32]Longest Valid Parentheses

题目描述: 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 exampl

LeetCode 032 Longest Valid Parentheses

题目描述:Longest Valid Parentheses 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 l

leetCode 32.Longest Valid Parentheses (有效的最大括号) 解题思路和方法

Longest Valid Parentheses 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