20、Valid Parentheses

Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. 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的更多相关文章

  1. [Leetcode][Python]20: Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...

  2. 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  3. LeetCode:20. Valid Parentheses(Easy)

    1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')',  ...

  4. 【一天一道LeetCode】#20. Valid Parentheses

    一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  5. LeetCode题解(20)--Valid Parentheses

    https://leetcode.com/problems/valid-parentheses/ 原题: Given a string containing just the characters ' ...

  6. 【LeetCode】20. Valid Parentheses 有效的括号

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...

  7. 【LeetCode】20. Valid Parentheses

    题目:

  8. LeetCode解题笔记 - 20. Valid Parentheses

    这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...

  9. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

随机推荐

  1. matlab中的科学记数法变成小数形式

    例如:假如rectx的形式在命令窗口显示: rectx = 1.0e+05 * 5.2294 5.2294 5.2294 5.2294 5.2294 那么,命令窗口输入vpa(rectx): ans ...

  2. Logiscope学习网址

    Logiscope测试机理   http://blog.csdn.net/cmy673986/article/details/9163247 http://www.cnitblog.com/qiuya ...

  3. 【Educational Codeforces Round 38】D. Buy a Ticket 堆优化Dijkstra

    题意 给定一张无向图,对每个点$i\in S$求$\min_{j\in S} {2\times d(i,j)+a_j}$ 考虑多源多汇最短路会超时,换个角度考虑每个$j$,如果$j=i$,那么答案为$ ...

  4. hdu-1025 Constructing Roads In JGShining's Kingdom(二分查找)

    题目链接: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)     Memory Li ...

  5. rtmp与hls流媒体服务器搭建:ubuntu下Nginx搭建初探与rtmp-module的添加

    关键词:Nignx(http服务器):rtmp,hls(流媒体服务) 前言:感谢开源,感谢战斗民族.现在在做流媒体服务的一些工作,流媒体服务器搭建的网上教程多入牛毛,但是细细查看,发现很多同志贴上来的 ...

  6. React 版 V2EX 社区( react & react-router & axios & antd ui)

    目录 项目简介 在线演示 截图演示 踩坑 项目简介(1/4) Github: https://github.com/bergwhite/v2ex-react 项目使用React.Reac-router ...

  7. 汇编题目:在窗口上显示Welcome to masm!

    题目:在屏幕中间分别显示绿色.绿底红色.白底蓝色的字符串'welcome to masm!'. 该程序题目来自<王爽 汇编语言_第2版>的188页的说明.相关资料也在上面都有详细说明. 题 ...

  8. Percona Xtrabackup 备份MySQL 实例(转)

    老规矩,开场白,刚开始用mysqldump,备份100G+的数据库,再加上服务器繁忙,备份速度像蜗牛似的,于是寻找更高效的备份方法.网上都说用xtrabackup比较适合备份大的数据库,而且备份效率也 ...

  9. JUST第二界算法设计大赛题解

    1.问题描述: 悠悠假期同叔叔一起去书店,他选中了六本书,每本书的单价(单位:元)分别为:3.1,1.7,2,5.3,0.9 和7.2.不巧的是,叔叔只带了十几块钱,为了让悠悠高兴,叔叔同意买书,但提 ...

  10. Uboot启动参数说明

    bootcmd=cp.b 0xc4200000 0x7fc0 0x200000 ; bootm // 倒计时到 0 以后,自动执行的指令 bootdelay=2 baudrate=38400 // 串 ...