国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] Valid Parentheses

AlphaWallet / 2935人閱讀

摘要:循環(huán)每個(gè)元素放入堆棧或比較是否和棧頂元素成對(duì)。循環(huán)結(jié)束后,若未拋出,且堆棧為空,說(shuō)明所有都已一一對(duì)應(yīng)。

Problem

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.

Note

建立堆棧stack。循環(huán)每個(gè)元素放入堆棧或比較是否和棧頂元素成對(duì)。
循環(huán)結(jié)束后,若未拋出false,且堆棧為空,說(shuō)明所有parenthese都已一一對(duì)應(yīng)。

Solution HashMap+Switch/case
public class Solution {
    public boolean isValid(String s) {
        Map map = new HashMap<>();
        map.put("(", ")");
        map.put("[", "]");
        map.put("{", "}");
        Stack stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            Character ch = s.charAt(i);
            switch (ch) {
                case "{": case "[": case "(":
                    stack.push(ch);
                    break;
                case ")": case "}": case "]":
                    if (stack.isEmpty() || ch != map.get(stack.pop())) return false;
            }
        }
        return stack.isEmpty();
    }
}
if-else branches
public class Solution {
    /**
     * @param s: A string
     * @return: whether the string is a valid parentheses
     */
    public boolean isValidParentheses(String s) {
        char[] str = s.toCharArray();
        if (str.length % 2 != 0) return false;
        Stack stack = new Stack<>();
        for (char ch: str) {
            if (ch == "(" || ch == "[" || ch == "{") {
                stack.push(ch);
            } else {
                if (stack.isEmpty()) {
                    return false;
                } else {
                    char top = stack.pop();
                    if ((ch == ")" && top != "(") || (ch == "]" && top != "[") || (ch == "}" && top != "{")) {
                        return false;
                    }
                }
            }
        }
        return stack.isEmpty();
    }
}

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

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

相關(guān)文章

  • [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
  • [Leetcode] Valid Parentheses 驗(yàn)證有效括號(hào)對(duì)

    摘要:如棧中是,來(lái)一個(gè)變成,再來(lái)一個(gè),變成。注意棧在或者操作之前要驗(yàn)證非空,否則會(huì)拋出。代碼最后要判斷棧的大小,如果循環(huán)結(jié)束后棧內(nèi)還有元素,說(shuō)明也是無(wú)效的代碼 Valid Parentheses Given a string containing just the characters (, ), {, }, [ and ], determine if the input string is...

    zhkai 評(píng)論0 收藏0
  • leetcode32 Longest Valid Parentheses 最長(zhǎng)括號(hào)組的長(zhǎng)度

    摘要:題目要求原題地址一個(gè)括號(hào)序列,求出其中成對(duì)括號(hào)的最大長(zhǎng)度思路一使用堆棧這題可以參考我的另一篇博客這篇博客講解了如何用堆棧判斷括號(hào)序列是否可以成對(duì)。我們可以將堆棧的思路延續(xù)到這里。在這里需要先遍歷一遍字符串,再遍歷一下非空的堆棧。 題目要求 原題地址:https://leetcode.com/problems... Given a string containing just the c...

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

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

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<