20. Valid Parentheses - 括号匹配验证
Description:
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
Example:
The brackets must close in the correct order,"()"
and"()[]{}"
are all valid but"(]"
and"([)]"
are not.
思路分析:
1.这个问题不仅仅是要求 ‘(’字符 与 ‘)’字符 需要成对出现,而且要求“开闭”紧凑,所以来回遍历逐个查找匹配肯定不行;
2.分析问题的特征,最主要的就是合法的输入是很规范的“有开有闭”类型,即能够通过在字符间插入特定数目的竖线,使得整个字符串各个局部都对称,对‘栈’敏感一点的其实能够慢慢联想到这一数据结构,一进一出、两进两出...其实都展示着一种对称;
3.因此,合法的输入一定可以通过‘栈’这一数据结构的pop,push操作完成一个个完整的进出过程。
代码思路:
step1:初始化栈,并入栈输入串的第一个字符;
step2: 从输入串的第二个字符到最后一个字符,依次与栈顶元素对比,栈不为空且栈顶元素与字符匹配则出栈,否则入栈该字符;
Step3: 操作完最后一个字符后,如果栈为空(即有进必有出,各个局部均对称),则输入合法;
C#代码:
public class Solution {
public bool IsValid(string s) {
//step1
Stack<char> stk = new Stack<char>();
char[] sArr = s.ToCharArray();
stk.Push(sArr[]);
for(int i = ;i<s.Length;i++){
//step2
if(stk.Count!=&&IsMatch(stk.Peek(),sArr[i])){
stk.Pop();
}else{
stk.Push(sArr[i]);
}
}
//step3
return stk.Count==;
}
public bool IsMatch(char a,char b){
if((a=='('&&b==')')||(a=='['&&b==']')||(a=='{'&&b=='}')) return true;
return false;
}
}
20. Valid Parentheses - 括号匹配验证的更多相关文章
- 20. Valid Parentheses(括号匹配,用桟)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- leetcode 20 Valid Parentheses 括号匹配
Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- 刷题20. Valid Parentheses
一.题目说明 这个题目是20. Valid Parentheses,简单来说就是括号匹配.在学数据结构的时候,用栈可以解决.题目难度是Medium. 二.我的解答 栈涉及的内容不多,push.pop. ...
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- leetCode练题——20. Valid Parentheses
1.题目 20. Valid Parentheses——Easy Given a string containing just the characters '(', ')', '{', '}', ...
- 20. Valid Parentheses【leetcode】
20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
随机推荐
- Iterator——迭代接口
迭代对于JAVA的来说绝对不陌生.我们常常使用JDK提供的迭代接口进行Java集合的迭代. Iterator iterator = list.iterator(); while(iterator.ha ...
- jenkins全局安全设置
如何进入安全设置界面 在Jenkins的主界面,点击 configure Global Security 选项,进入Jenkins的系统安全设置界面.安全界面如下图.在这里我们分别介 ...
- java作业—3
动手动脑: 一.编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数. 方法1(数据类型)(最小值+Math.random()*(最大值-最小值+1)) 例:(int)(1+Math.r ...
- 理解InnoDB的事务隔离级别
隔离是ACID(Atomicity,Consistency,Isolation,Durability)的重要部分,它保证事务以一种可靠的方式运行.隔离确保同时运行的事务并不相互干扰.隔离确保数据一致性 ...
- Python 接口测试(一)
@font-face { font-family: "Times"; }@font-face { font-family: "宋体"; }@font-face ...
- [Kafka] - Kafka 安装介绍
Kafka是由LinkedIn公司开发的,之后贡献给Apache基金会,成为Apache的一个顶级项目,开发语言为Scala.提供了各种不同语言的API,具体参考Kafka的cwiki页面: Kafk ...
- Sqlite 帮助类 SQLiteHelper
///源码下载地址:http://download.csdn.net/detail/kehaigang29/8836171 ///dll下载地址:http://download.csdn.net/de ...
- P177 test 6-4 UVa439
//P177 test 6-4 UVa439 #include<cstdio> #include<cstring> #include<queue> using na ...
- JNI调用的helloworld(JNI_OnLoad映射方式)
本示例展示JNI的基本示例,helloworld级别的,不过是用JNI_OnLoad映射的方式. 直接看代码,先看包含native method的Person.java的代码: package hel ...
- SUN SERVER X3-2 服务器数据写入缓慢
使用一台sun server x3-2,SAS 300G 2.5寸硬盘两块:8G内存条*2,CPU E5-2609V3 安装一套服务器系统时感觉安装进度很慢,但一直找不到原因,因为要重做系统,同事练手 ...