LeetCode OJ:Valid Parentheses(有效括号)
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.size())
return true;
char c;
stack<char> stk;
for(int i = ; i < s.size(); ++i){
if(s[i] == '[' || s[i] == '{' || s[i] == '('){
stk.push(s[i]);
continue;
}else if(s[i] == ']'){
if(stk.empty())
return false;
c = stk.top();
stk.pop();
if(c != '[')
return false;
}else if(s[i] == '}'){
if(stk.empty())
return false;
c = stk.top();
stk.pop();
if(c != '{')
return false;
}else{
if(stk.empty())
return false;
c = stk.top();
stk.pop();
if(c != '(')
return false;
}
}
if(stk.empty())
return true;
return false;
}
};
简单一点的话可以使用下面这种方式:
bool ValidParentheses(string s)
{
if (!s.size())
return true;
stack<char> stk;
map<char, char> m;
m['('] = ')';
m['['] = ']';
m['{'] = '}';
for (auto c : s){
if (c == '(' || c == '[' || c == '{'){
stk.push(c);
}
else if (c == ')' || c == ']' || c == '}'){
if (stk.empty())
return false;
if (c == m[stk.top()])
stk.pop();
else
return false;
}
}
if (stk.empty())
return true;
return false;
}
java版本代码如下所示:
public class ValidParentheses {
public static void main(String[] args) {
// TODO Auto-generated method stub
ValidParentheses validParentheses = new ValidParentheses();
String string = new String("[]{}(){[]}");
System.out.println("" + validParentheses.ValidParentheses(string));
}
boolean ValidParentheses(String str){
if(str.length() == 0)
return true;
Stack<Character> stk = new Stack<Character>();
char [] parentheses = str.toCharArray();
for(char c : parentheses){
if(c == '(' || c == '{' || c == '['){
stk.push(c);
}else if(c == ')'){
if(stk.empty() || stk.pop() != '(')
return false;
}else if(c == '}'){
if(stk.empty() || stk.pop() != '{')
return false;
}else if(c == ']'){
if(stk.empty() || stk.pop() != '[')
return false;
}
}
if(stk.empty())
return true;
return false;
}
}
LeetCode OJ:Valid Parentheses(有效括号)的更多相关文章
- [LeetCode] 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 inpu ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 20 Valid Parentheses (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
- 【LeetCode】Valid Parentheses合法括号
给定一个仅包含 '('.')'.'{'.'}'.'['.']'的字符串,确定输入的字符串是否合法. e.g. "()"."()[]{}"."[()]( ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [Leetcode] valid parentheses 有效括号对
Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...
随机推荐
- android6.0获取通讯录权限
android6.0中,获取通讯录的权限是 <uses-permission android:name="android.permission.GET_ACCOUNTS" ...
- eslasticsearch操作集锦
索引-index:一个索引就是一个拥有几分相似特征的文档的集合.比如说,你可以有一个客户数据的索引,另一个产品目录的索引,还有一个订单数据的索引.一个索引由一个名字来标识(必须全部是小写字母的),并且 ...
- flask实现获取表单并执行shell
1.一个HTML form input和一个button提供给用户输入 2.使用flask的request获取用户输入的文件名 3.判断输入异常 4.执行shell命令touch aa.txt 并返回 ...
- c++ ScopeExitGuard
说到Native Languages就不得不说资源管理,因为资源管理向来都是Native Languages的一个大问题,其中内存管理又是资源当中的一个大问题,由于堆内存需要手动分配和释放,所以必须确 ...
- xpath(待补充)
from lxml import etree html=""" <div> <ul> <li>1</li> <li ...
- java线程小结1
1.创建线程的两种方法 新线程的创建和启动都是通过java代码触发的.除了第一个线程(也就是启动程序的.运行main()方法的线程)是由java平台直接创建的之外,其余的线程都是在java代码中通过“ ...
- LeetCode:逆波兰表达式求值【150】
LeetCode:逆波兰表达式求值[150] 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除 ...
- redis 系列文章推荐
推荐博客: Redis在linux上的安装: http://www.open-open.com/lib/view/open1426468117367.html Redis的三种启动方式: http:/ ...
- 【JavaScript】写代码前的准备
1.搭建开发环境,编辑器推荐HBuilder,浏览器用谷歌. 2.编写一个HelloWorld程序. HelloWorld.html <!DOCTYPE html> <html> ...
- 20145222黄亚奇《网络对抗》MSF基础应用
实践目标 掌握metasploit的基本应用方式. 具体需要完成(1)ms08_067;(2)ms11_050:(3)Adobe(4)成功应用任何一个辅助模块. 实验内容 掌握metasploit的基 ...