溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

[LeetCode]20. Valid Parentheses

發(fā)布時間:2020-04-04 11:48:58 來源:網(wǎng)絡(luò) 閱讀:503 作者:風(fēng)子余 欄目:編程語言

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


題意:

括號字符串的匹配。


棧的特性:

后進(jìn)先出。


棧的頭文件:

struct stack

{

    char elem;

    struct stack *next;

};

棧的大?。?/p>

在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);
}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI