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(有效括号)的更多相关文章

  1. [LeetCode] 20. Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  2. [LeetCode] 20. Valid Parentheses 合法括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  3. [leetcode]20. Valid Parentheses有效括号序列

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  4. LeetCode 20 Valid Parentheses (括号匹配问题)

    题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description   Problem: 括号匹配问题. 使用栈,先进后出!   ...

  5. 【LeetCode】Valid Parentheses合法括号

    给定一个仅包含 '('.')'.'{'.'}'.'['.']'的字符串,确定输入的字符串是否合法. e.g. "()"."()[]{}"."[()]( ...

  6. [Leetcode] longest valid parentheses 最长的有效括号

    Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...

  7. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  8. [LeetCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  9. [Leetcode] valid parentheses 有效括号对

    Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...

  10. leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法

    Valid Parentheses  Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...

随机推荐

  1. HTML 块级元素与行内元素

    1.块元素一般都从新行开始,它可以容纳内联元素和其他块元素,常见块元素是段落标签'P".“form"这个块元素比较特殊,它只能用来容纳其他块元素. 2.如果没有css的作用,块元素 ...

  2. LeetCode:前K个高频元素【347】

    LeetCode:前K个高频元素[347] 题目描述 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [ ...

  3. Android位置权限以及数组寻找索引的坑

    填坑与求解惑来的. 一.Android 危险权限,来自官方文档的坑??? Android开发者都知道,Android 6.0 之前,权限申请只需要在 AndroidManifest.xml 文件中声明 ...

  4. C/C++中0xcccccccc...

    * 0xABABABAB : Used by Microsoft's HeapAlloc() to mark "no man's land" guard bytes after a ...

  5. 调试ASP.NET网站IIS环境问题解决方法汇总

    调试网站时出现错误,错误如下: 1. 分析器错误消息: 创建 RewriterConfig 的配置节处理程序时出错: 无法生成临时类(result=1).error CS2001: 未能找到源文件“C ...

  6. python处理时间相关的方法

    记录python处理时间的模块:time模块.datetime模块和calendar模块. python版本:2.7 https://blog.csdn.net/songfreeman/article ...

  7. git-bash使用ctrl C无法终止nodemon的执行

    原因: git的bug 解决:git版本降级为2.10.0好了

  8. jQuery上下切换带缩略图的焦点图

    在线演示 本地下载

  9. golang解析json报错:invalid character '\x00' after top-level value

    golang解析json报错:invalid character '\x00' after top-level value 手动复制字符串:{"files":["c:/t ...

  10. React Native之持久化存储(AsyncStorage、react-native-storage)的使用

    AsyncStorage是一个简单的.异步的.持久化的Key-Value存储系统,它对于App来说是全局性的.这是官网上对它的介绍.可以知道,这个asyncstorage也是以键值对的形式进行存储数据 ...