leetcode20

public class Solution {
    Stack<char> S = new Stack<char>();

        public bool IsValid(string s)
        {
            foreach (var c in s)
            {
                if (c == ‘(‘ || c == ‘[‘ || c == ‘{‘)
                {
                    S.Push(c);
                }
                else if(c == ‘)‘)
                {
                    if (S.Count > 0)
                    {
                        var k = S.Peek();
                        if (k == ‘(‘)
                        {
                            S.Pop();
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                else if (c == ‘]‘)
                {
                    if (S.Count > 0)
                    {
                        var k = S.Peek();
                        if (k == ‘[‘)
                        {
                            S.Pop();
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                else if (c == ‘}‘)
                {
                    if (S.Count > 0)
                    {
                        var k = S.Peek();
                        if (k == ‘{‘)
                        {
                            S.Pop();
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            if (S.Count > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
}

https://leetcode.com/problems/valid-parentheses/#/description

时间: 2024-08-05 14:10:13

leetcode20的相关文章

LeetCode20: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 "([)]"

LeetCode20——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 "([)]"

3.LeetCode20 Valid Parentheses 笔记

1:题目描述 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 "([

LeetCode20——有效的括号

在记事本中写算法题和在纸上写其实感觉差不多,反正是不能进行调试.想起某高手的话,写代码要做到"人机合一",写高级语言时(指的是 C 和 C++)脑海中要知道当前写的代码对应的反汇编代码,也就是要深入了解编译器对高级语言的处理.什么时候能达到这样的境界呢? LeetCode 题库的第 20 题--有效的括号 我做题的习惯跟考试的习惯差不多,先找会做的,然后再慢慢啃不会的.本着一个原则,不用编译器,不去找答案,不会说明基础不牢固,继续补基础. 题目我截图过来. 上面的图是题目和给出的示例,

LeetCode20 栈&#183;有效的括号(C++)

题目描述: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认为是有效字符串. 示例 1: 输入: "()" 输出: true 示例 2: 输入: "()[]{}" 输出: true 示例 3: 输入: "(]" 输出: false 示例 4: 输入: "([)]" 输出: fa

leetcode20. 有效的括号 &#127775;

题目: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认为是有效字符串. 示例 1: 输入: "()" 输出: true示例 2: 输入: "()[]{}" 输出: true示例 3: 输入: "(]" 输出: false示例 4: 输入: "([)]" 输出: false示例

&lt;每日 1 OJ&gt; -LeetCode20. 有效的括号

题目: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合.左括号必须以正确的顺序闭合.注意空字符串可被认为是有效字符串. 示例 1: 输入: "()"输出: true示例 2: 输入: "()[]{}"输出: true示例 3: 输入: "(]"输出: false示例 4: 输入: "([)]"输出: false示例 5: 输入

leetcode刷题题目分类

字符串 1.KMP字符串匹配 https://leetcode.com/problems/implement-strstr/description/ 2.句子逆序 leetcode151 3.判断一个树是否是另一个树的子树  leetcode242 括号 1.是否是有效括号 leetcode20 2.生成有效的括号序列 leetcode22 3.寻找字符串中最长有效括号序列长度 leetcode32 4.去除无效括号 leetcode301 5.添加括号的不同种方法 leetcode241 原文

算法训练营

概念:算法与数据结构相辅相成 算法是为了解决某一个具体的问题,提出来的一个解法 数据结构是为了支撑这次解法,所提出的一种存储结构 1.两数之和(LeetCode1) 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] =