[LintCode] 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.
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 验证括号的更多相关文章
- [LeetCode] 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有效括号序列
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 ...
- LintCode: Valid Parentheses
C++ stack<char|int|string>, push(), pop(), top(), empty(), size() class Solution { public: /** ...
- 20. Valid Parentheses检验括号字符串的有效性
[抄题]: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...
- 2.Valid Parentheses (括号匹配)
Level: Easy 题目描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- [LintCode] Valid Number 验证数字
Validate if a given string is numeric. Have you met this question in a real interview? Yes Example & ...
随机推荐
- Effective C++ 之 0 导读(Introduction)
Effective C++ 导读 (Introduction) 术语(terminology) 声明式 (declaration) 是告诉编译器某个东西的名称和类型(type),但略去细节.以下都是声 ...
- LeetCode——Single Number(找出数组中只出现一次的数)
问题: Given an array of integers, every element appears twice except for one. Find that single one. No ...
- PE文件的执行顺序
当一个PE文件被执行时,PE装载器首先检查DOS MZ header里的PE header的偏移量.如果找到,则直接跳转到PE header的位置. 当PE装载器跳转到PE header后,第二步要做 ...
- Eclipse首字母快捷设置
windows->Preference ,然后:
- .net发邮件【转】
对于.NET而言,从2.0开始,发邮件已经是一件非常easy 的事了.下面我给出一个用C#群发邮件的实例,做了比较详细的注解,希望对有需要的朋友有所help. // 引入命名空间using Syste ...
- JavaScript入门(2)
encodeURI()和 decodeURI()作用 编码与解码 encodeURIComponent()和 decodeURIComponent()作用区别是 后者可以处理一些特殊字符进行转义 ...
- javascript优化--12模式(设计模式)03
观察者模式 通过创建一个可观察的对象,当发生一个感兴趣的事件时将该事件通告给所有观察者,从而形成松散的耦合 订阅杂志 //发布者对象 var publisher = { subscribers: { ...
- PHP 字符检测自定义函数
<?php /** * 转义字符替换 * * @param string $subject * @return string */public static function sReplace( ...
- js兼容方法:事件添加|事件绑定|事件监听 addEvent
function addEvent(obj,sEvent,fn){ if(obj.attachEvent){ obj.attachEvent("on"+sEvent,fn); }e ...
- 点击 Run 之后发生了什么?
这是我以前去链家网面试的一个题目,当时回答不够全面,现在看了2016 WWDC以及Sunnyxx iDev大会的分享之后,感觉对这个问题有了一些简单的认识,就来总结下.如果大家有补充,麻烦评论下哦! ...