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.

Subscribe to see which companies asked this question

class Solution {
public:
bool isValid(string s) {
stack<char> store;
for (auto c : s) {
if (c == '(' || c == '{' || c == '[')
store.push(c);
else {
if (store.empty()) return false;
if (c == ')' && store.top() == '('){
store.pop();
}
else if (c == '}' && store.top() == '{'){
store.pop();
}
else if (c == ']' && store.top() == '['){
store.pop();
}
else
return false;
}
}
return store.empty();
}
};

Valid Parentheses的更多相关文章

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

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

  2. [LeetCode] Valid Parentheses 验证括号

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

  3. Longest Valid Parentheses

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

  4. 72. Generate Parentheses && Valid Parentheses

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

  5. leetcode 32. Longest Valid Parentheses

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

  6. 【leetcode】Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  7. 【leetcode】 Longest Valid Parentheses (hard)★

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

  8. [LintCode] Valid Parentheses 验证括号

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

  9. Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码

    Longest Valid Parentheses My Submissions Question Solution  Total Accepted: 47520 Total Submissions: ...

  10. [LeetCode] Longest Valid Parentheses 动态规划

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

随机推荐

  1. BZOJ3307 雨天的尾巴

    首先考虑序列怎么做... 只要把操作差分了,记录在每个点上 然后维护一棵权值线段树,表示每个颜色出现的次数,支持单点修改和查询最大值操作 只要把序列扫一遍就好了,时间复杂度$O(n + m*logZ) ...

  2. spring 标注

    1.添加支持标注的spring中的jar包: spring-context.jar spring-context-support.jar 2.在xml中配置命名空间和schema <beans ...

  3. linux 安装软件,卸载软件 等的几种方式

    安装软件的步骤和基本原则: 1. rpm安装: 定义: rpm是由红帽公司开发的软件包管理方式,使用rpm我们可以方便的进行软件的安装.查询.卸载.升级等工作.但是rpm软件包之间的依赖性问题往往会很 ...

  4. HADOOP 2架构图

    HDFS 2 architecture YARN architecture

  5. css默认样式

    html, address,blockquote,body, dd, div,dl, dt, fieldset, form,frame, frameset,h1, h2, h3, h4,h5, h6, ...

  6. iOS Android图标生成器PHP

    <?php //修改为你想要的大小 //$sizes = array(16,29,32,36,48,50,57,58,72,76,96,100,114,120,128,144,152); $si ...

  7. POJ 2762 tarjan缩点+并查集+度数

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15494 ...

  8. mysql给定一个随机数

    )) 给定一个1-50中间的随机数

  9. jquery插件理解看这

    zepto 插件写法 一个更换背景颜色的小插件 html 1 <div id="box">content</div> javascript 12345678 ...

  10. (BFS)hdoj2377-Bus Pass

    题目地址 因为最后要看的是到所有路线上的区域最大距离最小的中心点,所以可以采取遍历路线上所有的区域,对每个区域进行BFS的办法.为了更方便的在每一次BFS都遍历所有的区域,可以加一个reach数组,记 ...