有效的括號(C#)

給定一個只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串,判斷字符串是否有效。

有效字符串需滿足:

左括號必須用相同類型的右括號閉合。
左括號必須以正確的順序閉合。

注意空字符串可被認爲是有效字符串。

示例 1:

輸入: “()”
輸出: true

示例 2:

輸入: “()[]{}”
輸出: true

示例 3:

輸入: “(]”
輸出: false

示例 4:

輸入: “([)]”
輸出: false

示例 5:

輸入: “{[]}”
輸出: true

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/valid-parentheses
著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。

public class Solution
    {
        public bool IsValid(string s)
        {
            if (s == null)
                return true;
            Stack<char> temp = new Stack<char>();
            bool index=true;
            for (int i = 0; i < s.Length&&index==true; i++)
            {
                if (s[i] == '(' || s[i] == '{' || s[i] == '[')
                    temp.Push(s[i]);
                else if (temp.Count != 0)
                {
                    switch (s[i])
                    {
                        case ')':
                            if (temp.Peek() == '(')
                                temp.Pop();
                            else index = false;
                            break;
                        case '}':
                            if (temp.Peek() == '{')
                                temp.Pop();
                            else index = false;
                            break;
                        case ']':
                            if (temp.Peek() == '[')
                                temp.Pop();
                            else index = false;
                            break;
                    }
                }
                else index = false;
            }
            return temp.Count == 0 ? index : false;
        }
    }

注意最後只剩一個字符的情況,返回false。
改進:

public class Solution
    {
        public bool IsValid(string s)
        {
            if (s == null)
                return true;
            Stack<char> temp = new Stack<char>();
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == '(' || s[i] == '{' || s[i] == '[')
                    temp.Push(s[i]);
                else if (temp.Count != 0)
                {
                    switch (s[i])
                    {
                        case ')':
                            if (temp.Peek() == '(')
                                temp.Pop();
                            else return false;
                            break;
                        case '}':
                            if (temp.Peek() == '{')
                                temp.Pop();
                            else return false;
                            break;
                        case ']':
                            if (temp.Peek() == '[')
                                temp.Pop();
                            else return false;
                            break;
                    }
                }
                else return false;
            }
            return temp.Count == 0 ? true : false;
        }
    }

去掉了index.
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章