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 ...
随机推荐
- Luogu-4166 [SCOI2007]最大土地面积
求平面内四边形的最大面积 显然四个端点都应该在凸包上,就先求凸包,然后\(n^2\)枚举四边形对角线,对于一个点\(i\),顺序枚举\(j\),同时用旋转卡壳的方法去找离对角线最远的两个点.总时间复杂 ...
- 算法(Algorithms)第4版 练习 2.2.11(3)
关键代码实现: public static void sort(Comparable[] input) { int N = input.length; aux = input.clone();//mu ...
- 算法(Algorithms)第4版 练习 2.1.24
代码实现: package com.qiusongde; import edu.princeton.cs.algs4.In; import edu.princeton.cs.algs4.StdOut; ...
- Mysql异常_01_ 誓死登进mysql_Can't connect to MySQL server on 'localhost' (10061)
现象:打开cmd,输入命令:mysql -uroot -p 回车之后,输入密码,结果进不去mysql,并且抛出异常 异常:Can't connect to MySQL server on 'local ...
- 2018-06-07 RF test 1 :TX Power test
Test item: 1.Output power: 屏蔽网房-同轴线-频谱仪 The radio circuitry, generally referred to as the Device U ...
- BEC listen and translation exercise 45
So the Counselling Services we offer deal with any problems arising from your studies or in your lif ...
- TYVJ P1728 普通平衡树
P1728 普通平衡树 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 此为平衡树系列第一道:普通平衡树 描述 您需要写一种数据结构(可参考题目标题),来维护 ...
- 系列文章--Node.js学习笔记系列
Node.js学习笔记系列总索引 Nodejs学习笔记(一)--- 简介及安装Node.js开发环境 Nodejs学习笔记(二)--- 事件模块 Nodejs学习笔记(三)--- 模块 Nodejs学 ...
- centos 6.7 (UDEV,、etc/hosts)安装 RAC 11.2.0.4 报错处理
环境说明: db 11.2.0.4 os: centos 6.7 UDEV管理ASM 没有配置DNS服务器,采用/etc/hosts报错: 1. CVU(Cl ...
- Operating System-Process(1)什么是进程&&进程的创建(Creation)&&进程的终止(Termination)&&进程的状态(State)
本文阐述操作系统的核心概念之一:进程(Process),主要内容: 什么是进程 进程的创建(Creation) 进程的终止(Termination) 进程的状态(State) 一.什么是进程 1.1 ...