成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專(zhuān)欄INFORMATION COLUMN

leetcode 20 Valid Parentheses

qiangdada / 1445人閱讀

摘要:判定是否有效的一句就是,字符必須嚴(yán)格有序。例如和是有效的,但是和就是無(wú)效的。對(duì)于前一半字符,我們對(duì)它們進(jìn)行入棧操作。如果不匹配,即整個(gè)字符串無(wú)效。當(dāng)整個(gè)字符串的遍歷結(jié)束的時(shí)候,判斷棧是否為空完全匹配。

題目詳情
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.

輸入一個(gè)字符串s,這個(gè)字符串是由字符"(", ")", "{", "}", "[" 和 "]"組成的。我們需要判定這個(gè)字符串的格式是否有效。判定是否有效的一句就是,字符必須嚴(yán)格有序。例如"()" 和 "()[]{}"是有效的,但是"()"和"()[]{}"就是無(wú)效的。

想法

這道題目比較適合用棧作為存儲(chǔ)中間結(jié)果的數(shù)據(jù)結(jié)構(gòu),因?yàn)樽址麘?yīng)當(dāng)是后進(jìn)先出,方便我們進(jìn)行判斷。

對(duì)于前一半字符(eg.‘(’,"[","{"),我們對(duì)它們進(jìn)行入棧操作。

如果遇到了后半字符,我們需要對(duì)棧頂元素取出,并判斷棧頂字符和當(dāng)前字符是否匹配。如果不匹配,即整個(gè)字符串無(wú)效。

當(dāng)整個(gè)字符串的遍歷結(jié)束的時(shí)候,判斷棧是否為空(完全匹配)。

解法
        Stack save = new Stack();
        if(s.length() == 0)return true;
        
        for(char c : s.toCharArray()){
            if(c == "("){
                save.push(")");
            }else if (c == "["){
                save.push("]");
            }else if (c == "{"){
                save.push("}");
            }else if (save.isEmpty() || c != save.pop()){
                return false;
            }
        }
        
        return save.isEmpty();

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/76320.html

相關(guān)文章

  • LeetCode 20:有效的括號(hào) Valid Parentheses

    摘要:給定一個(gè)只包括,,,,,的字符串,判斷字符串是否有效。有效字符串需滿(mǎn)足左括號(hào)必須用相同類(lèi)型的右括號(hào)閉合。注意空字符串可被認(rèn)為是有效字符串。 給定一個(gè)只包括 (,),{,},[,] 的字符串,判斷字符串是否有效。 Given a string containing just the characters (, ), {, }, [ and ], determine if the inpu...

    TesterHome 評(píng)論0 收藏0
  • Leetcode20 - Valid Parentheses

    摘要:第一反應(yīng)是用棧,然后將左括號(hào)入棧,右括號(hào)出棧,遍歷結(jié)束后看看是不是棧空了。但是由于頻繁的函數(shù)調(diào)用,導(dǎo)致時(shí)間效率不如第一個(gè)。但是第一個(gè)的方法更容易出錯(cuò)。 Given a string containing just the characters (, ), {, }, [ and ], determine if the input string is valid. The br...

    iOS122 評(píng)論0 收藏0
  • [leetcode]Longest Valid Parentheses

    摘要:在問(wèn)題中,我們可以用來(lái)檢驗(yàn)括號(hào)對(duì),也可以通過(guò)來(lái)檢驗(yàn)。遇到就加一,遇到就減一。找到一對(duì)括號(hào)就在最終結(jié)果上加。我們用來(lái)表示當(dāng)前位置的最長(zhǎng)括號(hào)。括號(hào)之間的關(guān)系有兩種,包含和相離。 Longest Valid Parentheses Given a string containing just the characters ( and ), find the length of the lon...

    qujian 評(píng)論0 收藏0
  • [Leetcode] Longest Valid Parentheses 最長(zhǎng)有效括號(hào)對(duì)

    摘要:假設(shè)是從下標(biāo)開(kāi)始到字符串結(jié)尾最長(zhǎng)括號(hào)對(duì)長(zhǎng)度,是字符串下標(biāo)為的括號(hào)。如果所有符號(hào)都是,說(shuō)明是有效的。 Longest Valid Parentheses Given a string containing just the characters ( and ), find the length of the longest valid (well-formed) parentheses...

    everfight 評(píng)論0 收藏0
  • [LeetCode] 32. Longest Valid Parentheses

    Problem Given a string containing just the characters ( and ), find the length of the longest valid (well-formed) parentheses substring. Example 1: Input: (()Output: 2Explanation: The longest valid pa...

    Flink_China 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<