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. C#中var和dynamic

    var与dynamic这两个关键字,只是看起来很相似,仅此而已!var表示“变量的类型是在编译时决定的”,但是dynamic表 示“变量的类型是在运行时决定的”.因此,dynamic与var具有截然不 ...

  2. C# 获取wave文件信息【转】

    public class WaveHelper { /// <summary> /// 数据流 /// </summary> private Stream m_WaveData ...

  3. Fragment实现兼容手机和平板

    Android手机的设置界面,点击一下Sound,可以跳转到声音设置界面,如下面两张图所示:             然后再来看一下Android Pad的设置界面,主设置页面和声音设置页面都是在一个 ...

  4. BootCDN和npm

    稳定.快速.免费的开源项目 CDN 服务 BootCDN: jQuery3 <script type="text/javascript" src="http://c ...

  5. 【Tomcat 系统服务】将tomcat设置为系统服务,并且开机自启 + 卸载tomcat服务

    1.首先 你得下载一个tomcat[一般都是解压版的,解压放在那里就能用] startup.bat  shutdown.bat   service.bat等文件都在tomcat的bin目录下  ,例如 ...

  6. 【spring bean】 spring中bean之间的引用以及内部bean

    在spring中会有如下的几种情况: 1.在当前容器中,(即在spring.xml这一个配置文件中),一个bean引用了另一个bean. 使用 1>  <ref  bean="另 ...

  7. java net编程

    转自:http://www.cnblogs.com/linzheng/archive/2011/01/23/1942328.html 一,网络编程中两个主要的问题 一个是如何准确的定位网络上一台或多台 ...

  8. js从字符串中提取身份证号,连续18位数字

    <!DOCTYPE html> <html> <head> <title>提取身份证号</title> <meta charset=& ...

  9. Centos搭建Linux测试环境,几个基本的设置项

    一.添加sudo 权限 1.su - 切换至root,需要rootmim 2.修改 /etc/sudoers 的权限 chmod 644 /etc/sudoers 3.vi /etc/sudoers ...

  10. Codeforces 528D Fuzzy Search(FFT)

    题目 Source http://codeforces.com/problemset/problem/528/D Description Leonid works for a small and pr ...