当前位置:网站首页>Force deduction 32 questions longest valid bracket
Force deduction 32 questions longest valid bracket
2022-06-11 18:26:00 【Yingtai night snow】
32 Longest valid bracket
Title Description
Give you one that only contains '(' and ')' String , Find the longest effective ( The format is correct and continuous ) The length of the bracket substring .
I/o sample
Input :s = "(()"
Output :2
explain : The longest valid bracket substring is "()"
Input :s = ")()())"
Output :4
explain : The longest valid bracket substring is "()()"
Input :s = ""
Output :0
Solution a violent solution
- First, build a function that verifies whether it is a valid function
- Next, you can intercept the string for judgment , Intercept from large to small
- There are two ways , One is two for Loop head and tail traversal , One is based on the string 2 The properties of multiples of are reduced gradually
int longestValidParenthese(string s)
{
int maxlength=0;
int length=s.size();
if(length<=1)
{
return maxlength;
}
// double for Loop through
// for(int i=0;i<length;i++)
// {
// for(int j=length-1;j>=i;j--)
// {
// int lengthStr=j-i+1;
// string str=s.substr(i,lengthStr);
// if(isVaild(str))
// {
// maxlength=(lengthStr>maxlength)?lengthStr:maxlength;
// }
// }
// }
// Increase in logarithm
int moveLength=(length/2)*2;
int i=0;
string str;
for(i=0;moveLength>0;i++)
{
str=s.substr(i,moveLength);
if(isVaild(str))
{
maxlength=moveLength;
break;
}
if(i+moveLength>=length)
{
moveLength=moveLength-2;
i=-1;
}
}
return maxlength;
}
bool isVaild(string s)
{
stack<char>sta;
map<char,char>array={
{
')','('}};
for(auto i:s)
{
if(array.count(i))
{
if(sta.empty()||sta.top()!=array[i])
{
return false;
}
sta.pop();
}
else{
sta.push(i);
}
}
if(sta.empty())
{
return true;
}
else{
return false;
}
}
Solution 2 uses the dynamic programming method
int longestValidParentheses2(string s)
{
int maxlength=0;
int length=s.size();
// establish dp Array
vector<int>dp(length,0);
for(int i=1;i<length;i++)
{
// It is calculated according to whether the value is a right parenthesis
// If it is ) Update dp Array , Otherwise, do not update
if(s[i]==')')
{
// If the ) Corresponding ( Coordinates of exist
// And the value is exactly equal to (
if(s[i-1]=='(')
{
dp[i]=(i>=2?dp[i-2]:0)+2;
}
else if((i-dp[i-1]-1)>=0&&(s[i-dp[i-1]-1])=='(')
{
dp[i]=dp[i-1]+((i-dp[i-1]-2)>=0?(dp[i-dp[i-1]-2]):0)+2;
}
maxlength=max(maxlength,dp[i]);
}
}
return maxlength;
}
Solution 3 uses stack method
// The method of using stack
int longestValidParentheses3(string s)
{
int maxlength=0;
// The stack stores the coordinates
stack<int>stk;
// First of all -1 Initialization stack
// Represents the position of the first value
stk.push(-1);
for(int i=0;i<s.size();i++)
{
if(s[i]=='(')
{
stk.push(i);
}
// When s[i]=')', Execute out of stack
// And according to whether the stack is empty , Execute maximum length
else{
stk.pop();
// If the current stack is empty , Then put the current coordinates on the stack
if(stk.empty())
{
stk.push(i);
}
else{
maxlength=max(maxlength,i-stk.top());
}
}
}
return maxlength;
}
Solution 4: counting by brackets
int longestValidParentheses4(string s)
{
// Define the number of left and right parentheses
int left=0,right=0;
int maxlength=0;
int length=s.size();
for(int i=0;i<length;i++)
{
if(s[i]=='(')
{
left++;
}
else{
right++;
}
if(left==right)
{
maxlength=max(maxlength,2*right);
}
// When the number of right parentheses is greater than the number of left parentheses, the left and right parentheses count is reset
else if(right>left)
{
left=right=0;
}
}
// Traversal in reverse order
// Initializes the count value of the left and right parentheses 0
left=right=0;
for(int i=length-1;i>=0;i--)
{
if(s[i]=='(')
{
left++;
}
else{
right++;
}
if(left==right)
{
maxlength=max(maxlength,2*left);
}
// When the number of left parentheses is greater than the number of right parentheses, the left and right parentheses count is reset
else if(left>right)
{
left=right=0;
}
}
return maxlength;
}
边栏推荐
猜你喜欢

Async leads to unexpected function results and changes the intention of the original code; await is only valid in async functions and the top level bodies of modules

排序的循环链表
![[c language] output the students with the highest scores with a structure. There can be multiple highest scores](/img/4e/836a8f717a2d9bf5f999a934ff4c91.png)
[c language] output the students with the highest scores with a structure. There can be multiple highest scores

牛客刷题——合法括号序列判断

ISCSI详解(四)——ISCSI服务端配置实战

牛客刷题——两种排序方法
![[golang] leetcode - 349 Intersection of two arrays (hash table)](/img/92/03de54c9f08eae5bc920b04d2b8493.png)
[golang] leetcode - 349 Intersection of two arrays (hash table)

SISO decoder for a general (n, n-1) SPC code (supplementary Chapter 3)

* Jetpack 笔记 使用DataBinding
![[C语言]用结构体把最高分的学生输出,可有多个最高分](/img/4e/836a8f717a2d9bf5f999a934ff4c91.png)
[C语言]用结构体把最高分的学生输出,可有多个最高分
随机推荐
* Jetpack 笔记 LifeCycle ViewModel 与LiveData的了解
牛客刷题——二叉搜索树与双向链表
DataNode的启动流程
系统的可扩展型
System learning typescript (V) - joint type
[golang] leetcode - 292 Nim games (Mathematics)
[c language] output the average score and the student data below or equal to the average score with the structure
牛客刷题——part6
ubuntu 安装psql以及运行相关命令
论工作流选型
力扣33题,搜索旋转排序数组
H.264概念
Ubuntu installs PSQL and runs related commands
非递归实现二叉树的前、中、后序遍历
Understanding of distributed transactions
On the problem that the while loop condition in keil does not hold but cannot jump out
[C语言]限制查找次数,输出次数内查找到的最大值
EasyCwmp源码分析
牛客刷题——part8
判断是否为平衡二叉树