免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

[LeetCode]20.ValidParentheses

20. Valid Parentheses

成都創(chuàng)新互聯(lián)專注于企業(yè)網(wǎng)絡(luò)營銷推廣、網(wǎng)站重做改版、秀英網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5建站成都商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)公司、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價格優(yōu)惠性價比高,為秀英等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

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 all valid but "(]" and "([)]" are not.

題意:

括號字符串的匹配。


棧的特性:

后進(jìn)先出。


棧的頭文件:

struct stack

{

    char elem;

    struct stack *next;

};

棧的大小:

在64位系統(tǒng)上:sizeof(char) = 1,sizeof(struct stack *) = 8,sizeof(struct stack) = 16;

在32位系統(tǒng)上:sizeof(char) = 1,sizeof(struct stack *) = 4,sizeof(struct stack) = 8;

64位系統(tǒng)的地址是64位的,即8字節(jié);32位系統(tǒng)的地址是32的,即4字節(jié)。所以sizeof(struct stack *)分別是8字節(jié)和4字節(jié)。雖然sizeof(char)都為一字節(jié)。由于結(jié)構(gòu)體存在字節(jié)對齊,所以sizeof取出的結(jié)構(gòu)體大小是結(jié)構(gòu)體最大元素的整數(shù)倍。所以結(jié)構(gòu)體大小是16字節(jié)和8字節(jié)。

push入棧,pop出棧,top獲取棧頂元素。getLen函數(shù)如果棧為空,則返回一;否則返回零。

思路:

如果元素是'(','{'或'['則直接入棧;如果是元素是')',則判斷棧頂,如果棧頂元素是'('則'('出棧。'}'和']'一樣處理。

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

struct stack
*push(struct stack *head, char c)
{
    struct stack *node = NULL;
    node = (struct stack *)malloc(sizeof(struct stack));
    if ( node == NULL )
    {
        return NULL;
    }
    
    node->elem = c;
    if ( head == NULL )
    {
        node->next = NULL;
        head = node;
    }
    else
    {
        node->next = head;
    }
    
    return node;
}

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

struct stack
*pop(struct stack *head)
{
    if ( !head )
    {
        return NULL;
    }
    char val = head->elem;
    struct stack *delete = head;
    head = head->next;
    free(delete);
    return head;
}

int
getLen(struct stack *head)
{
    return head == NULL ? 1 : 0;
}

bool isValid(char* s) 
{
    struct stack *head = NULL;
    while ( *s )
    {
        if ( *s == '(' || *s == '{' || *s == '[' )
        {
            head = push(head, *s);
        }
        
        if ( *s == ')' )
        {
            if ( top(head) == '(' )
            {
                head = pop(head);
            }
            else
            {
                head = push(head, *s);
            }
        }
        
        if ( *s == '}' )
        {
            if ( top(head) == '{' )
            {
                head = pop(head);
            }
            else
            {
                head = push(head, *s);
            }
        }
        
        if ( *s == ']' )
        {
            if ( top(head) == '[' )
            {
                head = pop(head);
            }
            else
            {
                head = push(head, *s);
            }
        }
        s++;
    }
    
    return getLen(head);
}

文章標(biāo)題:[LeetCode]20.ValidParentheses
當(dāng)前URL:http://m.newbst.com/article12/jeiddc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)云服務(wù)器微信公眾號企業(yè)網(wǎng)站制作標(biāo)簽優(yōu)化網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)