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. ML三(人工神经网络)

    人工神经网络 Artificial Neural Nerworks 基本术语概念: 人工神经网络(Artificial Neural Networks,ANN) 感知器(Perceptron):以一个 ...

  2. 属性 visibility

    http://www.w3school.com.cn/cssref/pr_class_visibility.asp 可能的值 值 描述 visible 默认值.元素是可见的. hidden 元素是不可 ...

  3. 创建HTML5/CSS3单页Web布局

    1. [图片] 第1步:PhotoShop ​2. [代码]第2步:index.html <!DOCTYPE html><!-- The new doctype -->< ...

  4. PHP限制IP访问 只允许指定IP访问 允许*号通配符过滤IP

    /** * 检测访问的ip是否为规定的允许的ip * Enter description here ... */ function check_ip(){ $ALLOWED_IP=array('192 ...

  5. hbase_学习_00_资源帖

    一.官方资料 1.官网:http://hbase.apache.org/ 2.官方文档:HBase 官方文档中文版 二.apache软件下载基地 1. Apache Software Foundati ...

  6. C++0X 学习之 auto

    auto并不是一个新关键词,是一个把旧关键词赋予新的作用,新的作用修饰变量声明,指示编译器根据变量的初始化表达式推导变量应有的类型.auto 声明的变量必须“在声明处完成初始化”,编译器才可根据初始化 ...

  7. jQuery插件:图片放大镜--jQuery Zoom

    本文转载于http://blog.csdn.net/xinhaozheng/article/details/4085644, 这是一款非常不错的给图片添加放大镜效果,可以应用在诸如zen cart,m ...

  8. google android sdk下载hoosts

    203.208.46.146 www.google.com #这行是为了方便打开Android开发官网 现在好像不VPN也可以打开 74.125.113.121 developer.android.c ...

  9. 【LeetCode】014. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 题解: 简单的暴力遍历解决 ...

  10. BZOJ2548:[CTSC2002]灭鼠行动

    我对模拟的理解:https://www.cnblogs.com/AKMer/p/9064018.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem ...