C# 写 LeetCode easy #20 Valid Parentheses
20、Valid Parentheses
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example 1:
- Input: "()"
- Output: true
Example 2:
- Input: "()[]{}"
- Output: true
Example 3:
- Input: "(]"
- Output: false
Example 4:
- Input: "([)]"
- Output: false
Example 5:
- Input: "{[]}"
- Output: true
- 代码:
- static void Main(string[] args)
- {
- string str = "({}[])";
- bool res = IsValid(str);
- Console.WriteLine(res);
- Console.ReadKey();
- }
- private static bool IsValid(string s)
- {
- if (s.Length % != ) return false;
- Dictionary<char, char> dic = new Dictionary<char, char>() {
- { ')','('},
- { ']','['},
- { '}','{'}
- };
- var stack = new Stack<char>();
- foreach (var str in s)
- {
- if (dic.ContainsKey(str))
- {
- if (stack.Count != && stack.Peek() == dic[str])
- {
- stack.Pop();
- }
- else
- {
- return false;
- }
- }
- else
- {
- stack.Push(str);
- }
- }
- return stack.Count == ;
- }
解析:
输入:字符串
输出:bool类型
思想:
首先,这种匹配问题,都放在栈中,栈特点是先进后出。这里以键值对的形式把匹配项存入字典中。另外奇数个字符是不对的。
其次,刚开始栈空,将向右方向的字符((,{,[)全部存入栈中,然后若出现一个向左方向的,就在栈中匹配,判断是否与栈顶元素匹配。若匹配,则移出。否则返回false。
最后,判断栈中是否将全部元素匹配完毕,即都被移出,返回bool结果。
时间复杂度:O(n)
C# 写 LeetCode easy #20 Valid Parentheses的更多相关文章
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode:20. Valid Parentheses(Easy)
1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')', ...
- 【一天一道LeetCode】#20. Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- LeetCode题解(20)--Valid Parentheses
https://leetcode.com/problems/valid-parentheses/ 原题: Given a string containing just the characters ' ...
- 【LeetCode】20. Valid Parentheses 有效的括号
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...
- 【LeetCode】20. Valid Parentheses
题目:
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
随机推荐
- Delphi中 为DBNavigator的按钮加中文
Delphi中 为DBNavigator的按钮加中文 /*Delphi中数据库控件DBNavigator使用起来不错,但是按钮上“+”.“-”等含义对于中国的用户不习惯,甚至不知道是什么含义.改成相应 ...
- Codeforces 358D Dima and Hares:dp【只考虑相邻元素】
题目链接:http://codeforces.com/problemset/problem/358/D 题意: 有n个物品A[i]摆成一排,你要按照某一个顺序将它们全部取走. 其中,取走A[i]的收益 ...
- 常见SQL函数需要注意的细节
版权声明:本文为博主原创文章,未经博主允许不得转载. 这是一位牛人让我们思考的问题,说实话当时真蒙了,函数虽然明白,但细化到这种程度,真的是叫不准啊,下面是几道比较典型的问题,和本人做的实验,不一定准 ...
- linux应用之xampp集成环境的安装及配置(centos)
1.xampp集成环境的下载 在xampp的官网上选择对应系统的版本进行下载,官网地址:https://www.apachefriends.org/zh_cn/index.html #wget htt ...
- GUI创建各常用控件(一)
首先,作个申明: 1.这是一个野路子非科班的小菜鸟的学习,故诚心欢迎批评指正(同时所述内容可能有误): 2.本人目前使用的Unity3D版本为 5.3.5: 言归正传! 事实上在开发过程中已经很少用G ...
- LSM Tree 学习笔记——本质是将随机的写放在内存里形成有序的小memtable,然后定期合并成大的table flush到磁盘
The Sorted String Table (SSTable) is one of the most popular outputs for storing, processing, and ex ...
- 原生JS实现淡入淡出效果(fadeIn/fadeOut/fadeTo)
淡入淡出效果,在日常项目中经常用到,可惜原生JS没有类似的方法,而有时小的页面并不值得引入一个jQuery库,所以就自己写了一个,已封装, 有用得着的朋友, 可以直接使用. 代码中另附有一个设置元素透 ...
- tomcat使用JDNI配置信息和使用信息。用于JDBC连接池
JNDI: JNDI(java Naming and Directory Interface),java命名和目录接口.JNDI的作用就是:在服务器上配置资源,然后通过统一的方式来获取配置的资源 在t ...
- QTableWidget设计原则
一.组成结构: 列表控件由水平表头(self.horizontalHeader()).垂直表头(self.verticalHeader())和单元格(QTableWidgetItem)组成 其中表头又 ...
- 【leetcode刷题笔记】N-Queens II
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...