有效的括号(C#)

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

有效字符串需满足:

左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。

注意空字符串可被认为是有效字符串。

示例 1:

输入: “()”
输出: true

示例 2:

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

示例 3:

输入: “(]”
输出: false

示例 4:

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

扫描二维码关注公众号,回复: 10948576 查看本文章

示例 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.
在这里插入图片描述

发布了30 篇原创文章 · 获赞 4 · 访问量 898

猜你喜欢

转载自blog.csdn.net/weixin_45776347/article/details/105025485
今日推荐