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 ...
随机推荐
- MySQL按时间查找
RecentMutations表的结构如图,现在的需求是需要查找到2017年09月08日前10天的变体总数: SQL语句:SELECT SUM(MutantNumber) FROM RecentMut ...
- 对ASIHTTPRequest的封装
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/quanqinayng/article/details/37659751 .h文件 // // Ht ...
- Springboot入门-配置异常页面
springboot中,可以使用org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer类来统一的处理异 ...
- Web Service简单demo
最近开发因需求要求需要提供Web Service接口供外部调用,由于之前没有研究过该技术,故查阅资料研究了一番,所以写下来记录一下,方便后续使用. 这个demo采用CXF框架进行开发,后续所提到的We ...
- sublime txet 3 python 开发环境安装配置
下载python 下载地址:https://www.python.org/downloads/windows/ 下载sublime text 3 下载地址:https://www.sublimetex ...
- python16_day06【类、RE模块、subprocess模块、xml模块、shelve模块】
一.shelve模块 import shelve # 基于pickle模块, d = shelve.open('shelve_test') class Test(object): def __init ...
- nginx常用
1.rewrite return 301 http://example.com$request_uri; rewrite ^ http://example.com permanent; 2.try_f ...
- HDU - 2819 Swap (二分图匹配-匈牙利算法)
题意:一个N*N的01矩阵,行与行.列与列之间可以互换.要求变换出一个对角线元素全为1的矩阵,给出互换的行号或列号. 分析:首先一个矩阵若能构成对角线元素全为1,那么矩阵的秩为N,秩小于N的情况无解. ...
- 发送邮件——stamplib
配置文email.ini件信息: [email]sender=xxxxxxxxxxxpwd=xxxxxxxxxxxxreciver=xxxxxxxxxxxxxpython 3.x代码如下: impor ...
- ubuntu 16.04安装navicat for mysql
下载地址:官网https://www.navicat.com/download 1.下载 navicat120_mysql_en_x64.tar.gz 文件 2.下载后移到/opt/下 3.解压ta ...