【leetcode】Longest Valid Parentheses

#include<iostream>
#include<stack>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

struct Node
{
    int key;
    char c;
    Node(int key,char c):key(key),c(c){};
};
class Solution1
{
public:
    int fun(string s)
    {
        stack<Node*>result;
        int i=0;
        vector<int>arr;
        while(i<s.size())
        {
            Node *node=new Node(i,s[i]);
            if(!result.empty())
            {
                char temp=result.top()->c;
                if(juge(temp,s[i]))
                {
                    arr.push_back(result.top()->key);
                    arr.push_back(i);
                    result.pop();

                }
                else
                    result.push(node);
            }
            else
                result.push(node);      
            i++;    
        }
        /*while(!result.empty())
        {
            cout<<result.top()->key<<' '<<result.top()->c<<endl;
            result.pop();
        }*/
        sort(arr.begin(),arr.end());
        for(auto it=arr.begin();it!=arr.end();it++)
            cout<<*it<<endl;
        cout<<"*****"<<endl;

        int max=1;
        int max_id=0;
        for(vector<int>::iterator i=arr.begin();i!=arr.end();i++)
        {
            auto j=i+1;
            auto t=i;
            int count=1;
            for(;j!=arr.end();j++)
            {                                   

                cout<<*j<<endl;
                cout<<*t<<endl;
                if(1!=((*j)-(*t)))
                    break;
                else
                {
                    count++;                
                }
                t=j;        
            }cout<<"HHH"<<endl;
            if(max<count)
            {

                max=count;
                max_id=*(j-1)-max+1;
            }

            if(j==arr.end())
                break;
            i=j-1;          
        }
        return max;
    }
    bool juge(char a,char b)
    {
        if(a=='('&&b==')'||a=='['&&b==']'||a=='{'&&b=='}')
            return true;
        else
            return false;
    }
};

class Solution2
{
public:
    int fun(string s)
    {
        stack<int>result;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]=='(')
                result.push(i);
            else if(!result.empty())
            {
                s[result.top()]='$';
                s[i]='$';
                result.pop();
            }
        }
        int length=0;
        int max=0;
        for(int i=0;i<s.size();i++)
        {

            if(s[i]=='$')
            {
                length++;
                if(max<length)
                    max=length;
            }
            else
                length=0;
        }
        return max;
    }
};

class Solution3
{
public:
    int fun(string a)
    {
        stack<Node>result;
        int max_id=0;
        Node node=Node(-1,')');     
        result.push(node);
        for(int i=0;i<a.size();i++)
        {

            Node node=Node(i,a[i]);
            if(a[i]=='(')
                result.push(node);
            else
            {
                if(result.top().c=='(')
                {
                    result.pop();
                    /*if(max<i-result.top().key)
                        max=i-result.top().key;*/
                    max_id=max(max_id,i-result.top().key);
                }
                else
                    result.push(node);
            }
        }
        return max_id;
    }
};


class Solution4 {
public:
    int fun(string s) 
    {
        int answer = 0, depth = 0, start = -1;
        for (int i = 0; i < s.size(); ++i)
        {
            if (s[i] == '(') 
            {
                ++depth;
            } 
            else
            {
                --depth;
                if (depth < 0) 
                {
                    start = i;
                    depth = 0;
                } 
                else if (depth == 0) 
                {
                    answer = max(answer, i - start);

                }
            }
        }
        cout<<"answer"<<answer<<endl;
        depth = 0;
        start = s.size();
        for (int i = s.size() - 1; i >= 0; --i) 
        {
            if (s[i] == ')')
            {
            ++depth;
            } 
            else 
            {
                --depth;
                if (depth < 0) 
                {
                    start = i;
                    depth = 0;
                }
                else if (depth == 0) 
                {
                    answer = max(answer, start - i);
                }
            }
        }
        cout<<"answer"<<answer<<endl;
        return answer;
    }
};
void main()
{
    Solution1 solution;
    string a("((()()");
    cout<<"max="<<solution.fun(a);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值