题目:

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

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

说明:

1)括号可嵌套,如{[()]}

实现:

 class Solution {
public:
bool isValid(string s) {
if(s.empty()) return false;//空字符串,false
int len=strlen(s.c_str());
if(len==) return false;//只有一个字符,false
char pre;
stack<char> char_stack;
for(int i=;i<len;i++)
{
if(isPop(s[i])) //该出栈顶元素
{
if(!char_stack.empty())
{
pre=char_stack.top();//取栈顶元素、并出栈
char_stack.pop();
if(!isChar(pre,s[i]))//是否匹配
return false;
}
else return false;
}
else //入栈
{
char_stack.push(s[i]);
}
}
return char_stack.empty();//栈空true,否则false
}
private:
bool isPop(char t) //判断是否是)} ]符号,是则出栈栈顶元素
{
if(t==')'||t=='}'||t==']')
return true;
else
return false;
}
bool isChar(char t1,char t2)//判断两字符t1,t2是否匹配
{
switch (t1)
{
case '(':
{
if(t2==')') return true;
else return false;
break;
}
case '{':
{
if(t2=='}') return true;
else return false;
break;
}
case '[':
{
if(t2==']') return true;
else return false;
break;
}
default:
return false;
}
}
};

leetcode题解:Valid Parentheses(栈的应用-括号匹配)的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. 32. Longest Valid Parentheses(最长括号匹配,hard)

      Given a string containing just the characters '(' and ')', find the length of the longest valid (w ...

  3. leetcode 题解 || Valid Parentheses 问题

    problem: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...

  4. Leetcod--20. Valid Parentheses(极简洁的括号匹配)

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

  5. 栈应用之 括号匹配问题(Python 版)

    栈应用之 括号匹配问题(Python 版) 检查括号是否闭合 循序扫描被检查正文(一个字符)里的一个个字符 检查中跳过无关字符(所有非括号字符都与当前处理无关) 遇到开括号将其压入栈 遇到闭括号时弹出 ...

  6. [Leetcode] longest valid parentheses 最长的有效括号

    Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...

  7. [LeetCode] 32. Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  8. [LeetCode] 20. Valid Parentheses 验证括号

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

  9. [LeetCode] 20. Valid Parentheses 合法括号

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

  10. [leetcode]20. Valid Parentheses有效括号序列

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

随机推荐

  1. bzoj1266 [AHOI2006]上学路线route floyd建出最短路图+最小割

    1266: [AHOI2006]上学路线route Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 2490  Solved: 898[Submit][S ...

  2. 【Error】Python:UnicodeDecodeError: ‘XXX' codec can't decode bytes in position... 解决方法

    错误信息: UnicodeDecodeError: ‘XXX' codec can't decode bytes in position 2-5: illegal multibyte sequence ...

  3. 转:Android 的进程与线程总结

    当一个Android应用程序组件启动时候,如果此时这个程序的其他组件没有正在运行,那么系统会为这个程序 以单一线程的形式启动一个新的Linux 进程. 默认情况下,同一应用程序下的所有组件都运行再相同 ...

  4. codeforce 570 problem E&& 51Nod-1503-猪和回文

    1503 猪和回文 一只猪走进了一个森林.很凑巧的是,这个森林的形状是长方形的,有n行,m列组成.我们把这个长方形的行从上到下标记为1到n,列从左到右标记为1到m.处于第r行第c列的格子用(r,c)表 ...

  5. keystone总结

    1. Keystone(OpenStack Identity Service)是OpenStack框架中,负责身份验证.服务规则和服务令牌的功能, 它实现了OpenStack的Identity API ...

  6. MySQL 5.7主从复制与主主复制实现细节分析

    0.简介: MySQL作为世界上使用最为广泛的数据库之一,免费是其原因之一.但不可忽略的是它本身的功能的确很强大.随着技术的发展,在实际的生产环境中,由单台MySQL数据库服务器不能满足实际的需求.此 ...

  7. ssh xshell 连接在vim中无法用 ctrl+insert 复制黏贴

    在用户目录编辑.vimrc文件不存在则创建,vi的三种模式:命令模式,插入模式,可视模式,鼠标可以启动于各种模式中,所以配置文件中的set mouse=a启动了所有模式,这样就屏蔽了鼠标右健功能,se ...

  8. laravel中建立公共视图的方法

    1.用法概要 @include('common.header') 包含子视图 @extends('article.common.base') 继承基础模板 @yield('content') 视图占位 ...

  9. PHP成生若干位防伪码的方法

    public static function createCode($length =16) { $arr = array_merge(range(0, 9), range('a', 'z')); / ...

  10. MVC5下的switchbutton

    最近想提高代码生成的时候,是否选择显示样式好看些,所以想到了SwitchButton. 但是碰到个问题是,添加页面的这个初始值,需要写成true或者false 包括修改页面做个判断的话,渲染的做法也是 ...