【leetcode c++】 20 Valid Parentheses
来源:互联网 发布:时时彩源码程序出售 编辑:程序博客网 时间:2024/03/29 03:13
括号匹配。这题,是大学数据结构的题目吧,栈的题目吧(还有魔王语言、迷宫等,有木有)。因为是写过的题目,所以这次直接在leetcode的网页上面写了。还好还记住了思路,一次过~~
Leetcode的Accepted Solutions Runtime Distribution(2015-06-05)
源码:
bool isValid(string s){stack<int> myStack;string::iterator iter = s.begin();string::iterator iterEnd = s.end();while (iter != iterEnd){if ('(' == *iter || '[' == *iter || '{' == *iter)//左括号进栈{myStack.push(*iter);iter++;}else//遇到右括号了{if (!myStack.empty())//遇到右括号了,且栈不为空,有可能是Valid,继续往下{if (')' != *iter && myStack.top() == *iter - 2)//非小括号匹配成功,继续扫描{myStack.pop();iter++;}else if (')' == *iter && myStack.top() == *iter - 1)//小括号匹配成功,继续扫描{myStack.pop();iter++;}else return false;//匹配失败了}else return false;//遇到右括号且栈为空,必然匹配不上了}}//走到这里,说明右括号都配对了。if (iterEnd == iter && myStack.empty())//如果此时栈不为空,说明没有落单的左括号了,说明字符串完全匹配完毕{return true;}return false;}
0 0
- 【leetcode c++】 20 Valid Parentheses
- [C语言][LeetCode][20]Valid Parentheses
- [C语言][LeetCode][20]Valid Parentheses
- LeetCode 20: Valid Parentheses
- LeetCode(20) Valid Parentheses
- leetcode 20 Valid Parentheses
- Leetcode【20】:Valid Parentheses
- leetcode 20-Valid Parentheses
- [leetcode 20] Valid Parentheses
- [Leetcode] 20 - Valid Parentheses
- LeetCode | #20 Valid Parentheses
- leetcode.20-----------Valid Parentheses
- [leetcode] 20 Valid Parentheses
- leetcode-20 Valid Parentheses
- leetCode #20 Valid Parentheses
- leetcode 20 Valid Parentheses
- Leetcode 20 Valid Parentheses
- LeetCode 20 - Valid Parentheses
- 解决AD 不能打开DDB文件的解决方案
- keepalived 实现mysql主从自动切换
- 倒计时
- Xilinx FPGA ISE JTAG boundary scan 扫描出 unknown device的问题
- SQL知识积累——常用函数及特殊字符
- 【leetcode c++】 20 Valid Parentheses
- vs2013代码运行窗口一闪而过
- Android 程序框架设计
- [数据结构]线性表之单链表的类模板实现
- java for的执行顺序
- 关于struts2 select标签默认样式的修改
- poppler 心得 windows下的mfc应用和非mfc
- Asp.Net 详解IIS内部运行原理
- 百度面试相关问题