20. Valid Parentheses
2016-11-11 15:21
211 查看
Given a string containing just the characters
determine if the input string is valid.
The brackets must close in the correct order,
all valid but
not.
class Solution {
public:
bool isValid(string s) {
if (s.empty()) return true;
int cnt = s.size();
stack<char> temp;
for(int i = 0; i < cnt; ++i)
{
if(s[i] == '(' || s[i] == '{' || s[i] == '[')
temp.push(s[i]);
else
{
if(temp.empty())
return false;
if(s[i] == ')' && temp.top() == '(' || s[i] == '}' && temp.top() == '{' || s[i] == ']' && temp.top() == '[')
temp.pop();
else
return false;
}
}
return (temp.empty());
}
};
'(',
')',
'{',
'}',
'['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.empty()) return true;
int cnt = s.size();
stack<char> temp;
for(int i = 0; i < cnt; ++i)
{
if(s[i] == '(' || s[i] == '{' || s[i] == '[')
temp.push(s[i]);
else
{
if(temp.empty())
return false;
if(s[i] == ')' && temp.top() == '(' || s[i] == '}' && temp.top() == '{' || s[i] == ']' && temp.top() == '[')
temp.pop();
else
return false;
}
}
return (temp.empty());
}
};
相关文章推荐
- [leetcode] 20.Valid Parentheses
- leetcode 20. Valid Parentheses——python(easy)——使用了堆栈
- 20.Valid Parentheses
- 20. Valid Parentheses
- LeetCode 20_Valid Parentheses
- <LeetCode OJ> 20. Valid Parentheses
- LeetCode20——Valid Parentheses(括号匹配问题,使用栈的知识)
- LeetCode 20: Valid Parentheses
- LeetCode 20. Valid Parentheses
- 20. Valid Parentheses
- leetcode (20) - Valid Parentheses
- String-20-Valid Parentheses
- Leetcode_20_Valid Parentheses
- [leetcode]20. Valid Parentheses
- 【Leetcode problem 20】-Valid Parentheses
- 20. Valid Parentheses
- 【LeetCode】20. Valid Parentheses
- LeetCode 20: Valid Parentheses
- LeetCode 20. Valid Parentheses(合法括号序列)
- leetcode 20. Valid Parentheses