1. 程式人生 > >Valid Parentheses 合法的括號匹配

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.

題目大意:給定一個字串只包含“(){}[]”,判斷該字串的括號是否匹配

思路:利用棧的思想,不匹配入棧,匹配出棧,最後檢視棧頂

#include <string.h>

bool isValid(char* s) {
    int len = strlen(s);
    char *p = malloc(len);
    int top = 0;    //從下標1開始儲存
    
    if(!p)
        return false;
    memset(p, 0x0, len);
    while(*s)
    {
        //檢視assic碼,得知匹配情況的assic碼差值
        if(*s - p[top] == 1 || *s - p[top] == 2)
        {
            p[top] = 0;
            top--;
        }
        else
        {
            top++;
            p[top] = *s;

        }
        s++;
    }
    free(p);
    return (top ? false : true);
}


相關推薦

Valid Parentheses 合法括號

public boolean isValid(String s) { Stack<Character> stack = new Stack<Cha

Valid Parentheses 合法括號匹配

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets mu

【LeetCode 20. Valid Parentheses】(合法括號匹配判斷,棧的應用)

題目連結 Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. An in

[Leetcode] valid parentheses 有效括號

true class mine int min () etc strong bracket Given a string containing just the characters‘(‘,‘)‘,‘{‘,‘}‘,‘[‘and‘]‘, determine if the in

20. Valid Parentheses檢驗括號字符串的有效性

輸入 情況 brackets har 不存在 close ring HA IT [抄題]: Given a string containing just the characters ‘(‘, ‘)‘, ‘{‘, ‘}‘, ‘[‘ and ‘]‘, determine i

【LeetCode】20 Valid Parentheses 有效括號

給定一個只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字串,判斷字串是否有效。 有效字串需滿足: 左括號必須用相同型別的右括號閉合。 左括號必須以正確的順序閉合。 注意空字串可被認為是有效字串。 示例 1: 輸入: “()” 輸出: true 示例 2: 輸入: “()[

6、Valid Parentheses 驗證括號

                                                        Valid Parentheses Description: Given a stri

[LeetCode] Valid Parentheses 驗證括號

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the co

【LeetCode & 劍指offer刷題】字串題11:Valid Parentheses括號對)

【LeetCode & 劍指offer 刷題筆記】目錄(持續更新中...) Valid Parentheses Given a string containing just the characters   '('

20. Valid Parentheses括號匹配

ase action style spa break ive clas not str 20 Valid Parentheses Given a string containing just the characters ‘(‘, ‘)‘, ‘{‘, ‘}‘, ‘[‘

Valid Parentheses(括號匹配

nta body 括號匹配 ren 判斷 != pan val pre 解決思路: 1. 棧 2.使用Map,判斷是否 public boolean isValid(String s) { Stack<Character> parens =

20. Valid Parentheses(括號匹配,用桟)

char val solution 如果 close nth fix body -c Given a string containing just the characters ‘(‘, ‘)‘, ‘{‘, ‘}‘, ‘[‘ and ‘]‘, determine if

leetcode-20:Valid Parentheses有效的括號括號匹配

題目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the

LeetCode-20、22:Valid、Generate Parentheses(括號匹配、生成)

題目20:Valid Parentheses Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input strin

LeetCode 20 Valid Parentheses(用棧判斷括號匹配

Given a string containing just the characters'(',')','{','}','['and']', determine if the input str

LeetCode 20. Valid Parentheses 無效的括號匹配 Java易於理解的解法

題目 Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. 給定一個只包含

【棧操作&括號匹配Valid Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets m

【大風】matrix:valid parentheses括號匹配程式碼分享

Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. The

Leetcod--20. Valid Parentheses(極簡潔的括號匹配)

bsp for NPU peek pan color bool put tco Given a string containing just the characters ‘(‘, ‘)‘, ‘{‘, ‘}‘, ‘[‘ and ‘]‘, determine if the i

20. Valid Parentheses(用棧實現括號匹配)

ise || character output sam char span arr NPU Given a string containing just the characters ‘(‘, ‘)‘, ‘{‘, ‘}‘, ‘[‘ and ‘]‘, determine if