题目:

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. 刷题总结——Aeroplane chess(hdu4405)

    题目: Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labele ...

  2. mysql的乐观锁和悲观锁

    悲观锁与乐观锁是两种常见的资源并发锁设计思路,也是并发编程中一个非常基础的概念.本文将对这两种常见的锁机制在数据库数据上的实现进行比较系统的介绍. 悲观锁(Pessimistic Lock) 悲观锁的 ...

  3. 华中农业大学第四届程序设计大赛网络同步赛 J

    Problem J: Arithmetic Sequence Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1766  Solved: 299[Subm ...

  4. DP———7.导弹拦截(emmm冷静分析一波也不叫DP吧,不过有一种DP的方法写)

    最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. 51nod 1273 旅行计划——思维题

    某个国家有N个城市,编号0 至 N-1,他们之间用N - 1条道路连接,道路是双向行驶的,沿着道路你可以到达任何一个城市.你有一个旅行计划,这个计划是从编号K的城市出发,每天到达一个你没有去过的城市, ...

  6. How to Use Instruments in Xcode

    http://blog.csdn.net/woaifen3344/article/details/40748075 This is a blog post by iOS Tutorial Team m ...

  7. source insight setting

    adjust source code font size Options -> File Type Options -> Screen Font -> Size adjust dis ...

  8. Linux学习总结—缺页中断和交换技术【转】

    三.Linux缺页中断处理 转自:http://blog.csdn.net/cxylaf/article/details/1626534 1.请求调页中断: 进程线性地址空间里的页面不必常驻内存,例如 ...

  9. C++ 播放音频流(PCM裸流)--改善

    直接上代码,如果有需要可以直接建一个win32控制台程序然后将代码拷过去改个文件名就可以用了(注意将声道和频率与你自己的文件对应).当然我自己也用VS2008写了个例子上传了,如果有需要下载地址如下: ...

  10. hdu 1142(迪杰斯特拉+记忆化搜索)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...