leetcode之Valid Parentheses
来源:互联网 发布:mac虚拟机配置文件 编辑:程序博客网 时间:2024/06/05 15:51
原题如下:
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.
class Solution {public: bool isValid(string s) {if(s.length() == 0)return true;stack<char>st;for(int i = 0; i < s.length(); i++){if(s[i] == '(')st.push('(');else if(s[i] == '[')st.push('[');else if(s[i] == '{')st.push('{');else{if(st.empty())return false;char c = st.top(); if((c == '(' && s[i] == ')') || (c == '[' && s[i] == ']') || (c == '{' && s[i] == '}') )st.pop();elsereturn false;}}if(st.empty())return true;return false; }};注:在利用st.top()取栈顶元素时要确保栈不为空,否则出现运行时错误。
0 0
- leetcode之Valid Parentheses
- LeetCode之Valid Parentheses
- leetcode之Valid Parentheses
- LeetCode之Valid Parentheses
- 【LeetCode】之Valid Parentheses
- 【Leetcode】之Valid Parentheses
- leetcode之Valid Parentheses
- LeetCode之Valid Parentheses
- leetcode 之 Valid Parentheses
- leetcode 之 Longest Valid Parentheses
- LeetCode之Longest Valid Parentheses
- 【Leetcode】之Longest Valid Parentheses
- LeetCode之20---Valid Parentheses
- Leetcode之Longest Valid Parentheses
- leetcode之20. Valid Parentheses
- LeetCode算法题之Valid Parentheses
- LeetCode 之 Valid Parentheses — C++ 实现
- LeetCode 之 Longest Valid Parentheses(栈)
- hadoop安装 步骤(linux)
- android照片墙的实现
- android备份服务流程
- 在gdb下查看map,vector,queue的数据
- 用shell从文件中提取指定字符串的行
- leetcode之Valid Parentheses
- Android源码分析之工厂方法模式
- poj(3280)Cheapest Palindrome(区间dp)
- 类的sizeof
- oracle如何将数据导入到另一个表空间
- #pragma pack(n)的用法
- 数据存储模型
- 32位、64位与Java开发研究分析
- iOS:xcode Overview3 && structure & template & relationship & target