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

Have you met this question in a real interview?

 
 
Example

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

LeetCode上的原题,请参见我之前的博客Valid Parentheses

class Solution {
public:
/**
* @param s A string
* @return whether the string is a valid parentheses
*/
bool isValidParentheses(string& s) {
stack<char> st;
for (char c : s) {
if (c == ')' || c == '}' || c == ']') {
if (st.empty()) return false;
if (c == ')' && st.top() != '(') return false;
if (c == '}' && st.top() != '{') return false;
if (c == ']' && st.top() != '[') return false;
st.pop();
} else {
st.push(c);
}
}
return st.empty();
}
};

[LintCode] Valid Parentheses 验证括号的更多相关文章

  1. [LeetCode] 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有效括号序列

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

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

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

  6. LintCode: Valid Parentheses

    C++ stack<char|int|string>, push(), pop(), top(), empty(), size() class Solution { public: /** ...

  7. 20. Valid Parentheses检验括号字符串的有效性

    [抄题]: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...

  8. 2.Valid Parentheses (括号匹配)

    Level: ​  Easy 题目描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  9. [LintCode] Valid Number 验证数字

    Validate if a given string is numeric. Have you met this question in a real interview? Yes Example & ...

随机推荐

  1. loj 1032 数位dp

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1032 思路:数位dp, 采用记忆化搜索, dp[pos][pre][have] 表示 ...

  2. 数据分析(3):ufunc

    universal function 可以对数组里的每一个元素进行操作,底层是C语言实现的,在对数组运算时表现卓越 1.1 初步上手 x = np.linspace(0,2*np.pi,10) y = ...

  3. ObCallback回调钩子检测

    ObCallback回调钩子检测 2013-12-20 Nie.Meining Ring0 在 PatchGuard 的摧残下,通过 ObRegisterCallbacks 函数注册回调钩子已经成了 ...

  4. Android 开发工具之Codota

    Codota 的特性 的确,正如codota 官方所说,的确是精品,他的搜索源,不仅只有Github,而且还有知名博客和开发者网站,让你搜索一个东西,不用在找上半天: 除了搜索功能,首页的下方还罗列比 ...

  5. 【SQL 数据库】将一张数据表信息复制到另一张数据表

    一.MySQL数据库 1.如果目标表存在 INSERT INTO 目标表  SELECT  * FROM 源表; 2.如果目标表不存在 CREATE TABLE 目标表 SELECT * FROM   ...

  6. 【jackson 异常】com.fasterxml.jackson.databind.JsonMappingException异常处理

    项目中,父层是Gene.java[基因实体]  子层是Corlib.java[文集库实体],一种基因对用多个文集库文章 但是在查询文集库这个实体的时候报错:[com.fasterxml.jackson ...

  7. java运行jar命令提示没有主清单属性

    转自:http://jingyan.baidu.com/article/db55b60990f6084ba30a2fb8.html 可运行的jar:http://mushiqianmeng.blog. ...

  8. ps命令使用 进程查看

    ps命令是Process Status的缩写 用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信息,就可以使用top命 ...

  9. javascript关键字和保留字

    1 关键字breakcasecatchcontinuedefaultdeletedoelsefinallyforfunctionifininstanceofnewreturnswitchthisthr ...

  10. http://blog.csdn.net/lipeng32768/article/details/50845547

    http://blog.csdn.net/lipeng32768/article/details/50845547