资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

[LeetCode]32.LongestValidParentheses

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 example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.

题意:

给定字符串(字符串只包括 '(' 和 ')' 元素)。查找最长的有效字串,即 '(' 和 ')' 匹配的最长字串。

思路:
1)创建栈,碰到匹配的括号时,弹出栈顶元素;否则,。

2)创建数组,当出栈动作发生时,把到目前为止的入栈次数与出栈次数的差值存入数组中。

3)数组处理。获取最长字串。

A)字符串模式
)) (()) ) ((()))  对应数组为:3 2 5 4 3

B)字符串模式
)) ()() ) ()()()  对应数组为:2 2 3 3 3

C)字符串模式
)) () ) ()((()))  对应数组为:2 3 5 4 3

D)字符串模式
)) (()) ) (())()  对应数组为:3 2 4 3 3

由上可知:
模式匹配基本就只有
1,嵌套(())
2,平级()()

第一种方式的数组会出现递减方式,第二种方式的数组元素会出现保持不变的。

一旦出现不匹配的,那么只有push动作存在,遇到pop时中间push和pop的差肯定是增涨的。可是如果中间都是匹配的,那么最终push和pop的差不会涨。

获取最长字串的方法:
获取递减序列,纪录递减序列长度,并纪录递减序列开始的首元素和尾元素。从纪录的首元素开始往前查找,直到遇到的元素小于纪录的尾元素,记前驱长度。
递减序列长度+前驱长度 = 字串长度。

struct stack
{
    char word;
    struct stack *next;
};

struct stack
*push(struct stack *head, char word)
{
    struct stack *node = (struct stack *)malloc(sizeof(struct stack));
    if ( !node )
    {
        printf("create node error\n");
        return head;
    }
    
    node->word = word;
    if ( !head )
    {
        node->next = NULL;
        head = node;
    }
    else
    {
        node->next = head;
    }
    
    return node;
}

struct stack
*pop(struct stack *head)
{
    if ( !head )
    {
        return head;
    }
    
    struct stack *del = head;
    head = head->next;
    free(del);
    
    return head;
}

char
top(struct stack *head)
{
    if ( !head )
    {
        return 0;
    }
    
    return head->word;
}

void
stackFree(struct stack *head)
{
    if ( !head )
    {
        return;
    }
    
    struct stack *del = NULL;
    while ( head )
    {
        del = head;
        head = head->next;
        free(del);
    }
}

int
longestValidParentheses(char* s)
{
    if ( !s )
    {
        return 0;
    }
    
    int size = strlen(s) / 2 + 1;
    int sub[size];
    int index = 0;
    struct stack *head = NULL;
    int pushNum = 0;
    int popNum  = 0;
    int flag = 0;
    for ( ; *s; s++ )
    {   
        if ( *s == '(' )
        {   
            head = push(head, *s);
            pushNum += 1;
            flag = 0;
        }   
        else if ( *s == ')' )
        {   
            if ( top(head) == '(' )
            {   
                head = pop(head);
                popNum += 1;
                flag = 1;
            }   
            else
            {
                head = push(head, *s);
                pushNum += 1;
                flag = 0;
            }
        }
        if ( flag == 1 )
        {
            sub[index] = pushNum - popNum;
            index += 1;
        }
    }
    
    stackFree(head);
    
    if ( index == 1 )
    {
        return index * 2;
    }
    
    int length  = 0;
    int maxLen  = 0;
    int cnt = 0;
    int min = 0;
    for ( cnt = 0; cnt < index - 1; cnt++ )
    {
        length = 0;
        min    = -1;
        while ( (cnt + 1 + length) < index && sub[cnt + length] >= sub[cnt + 1 + length] )
        {
            length += 1;
        }
        
        while ( (cnt - 1 - min) >= 0 && sub[cnt - 1 - min] >= sub[cnt + length] )
        {   
            min += 1;
        }
        
        cnt = cnt + length;
        length = length + 1 + min;
        if ( length > maxLen )
        {
            maxLen = length;
        }
    }
    
    return maxLen * 2;
}

注意栈内存的释放


本文名称:[LeetCode]32.LongestValidParentheses
网站URL:http://cdkjz.cn/article/jshppp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220