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 ...
随机推荐
- use html5 video tag with MSE for h264 live streaming
本编博客记录桌面虚拟化移动端预研. 完整demo: https://github.com/MarkRepo/wfs.js 常见的直播方案有RTMP RTSP HLS 等等, 由于这些流都需要先传输到服 ...
- Linux基础整理 + 注释
1.Linux的常用命令: ls 显示当前文件夹目录 ll -->详细信息 ls -a 显示所有文件 ls -lhSr w 查看登录的帐号,还可以查看cpu负载情况,who am i ,who ...
- HDU1712:ACboy needs your help(分组背包)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1712 解释看这里:http://www.cnblogs.com/zhangmingcheng/p/3940 ...
- 设计模式(六) xml方式实现AOP
1.1. Aop, aspect object programming 面向切面编程 功能: 让关注点代码与业务代码分离! 关注点, 重复代码就叫做关注点: 切面, 关注点形成的类,就叫切面(类) ...
- CString和char互转,十六进制的BYTE转CString
CString转char: CString m_Readcard; char ReaderName[22]; strcpy((char*)&ReaderName,(LPCTSTR)m_Read ...
- C# ---sender
在某个方法中: 第一种写法: private void btn4_Click_1(object sender, RoutedEventArgs e) { btn1_Click(null, null); ...
- 史上最详细Windows版本搭建安装React Native环境配置
说在前面的话: 感谢同事金晓冰倾情奉献本环境搭建教程 之前我们已经讲解了React Native的OS X系统的环境搭建以及配置,鉴于各大群里有很多人反应在Windows环境搭建出现各种问题,今天就特 ...
- shell编程学习笔记之标准输入输出(read&echo)
2017-07-17 09:32:07 输入read: 用途: 从标准输入读取一行,或者从文件描述符FD(file descriptor)中读取一行,并且将其分割成字段. 用法: read [-ers ...
- Python 函数定义和使用
# 函数的概念 # 概念 # 写了一段代码实现了某个小功能; 然后把这些代码集中到一块, 起一个名字; 下一次就可以根据这个名字再次使用这个代码块, 这就是函数 # 作用 # 方便代码的重用 # 分解 ...
- eclipse——添加Tomcat7.0服务器
首先要安装好Tomcat 然后在eclipse中添加Tomcat 步骤如下 详细可参考这篇博客https://blog.csdn.net/u014079773/article/details/5139 ...