题目:
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.
题意:一行字符串,判断是否有括号的匹配。即搜索到左括号就应该去对应的找右括号。有点和字符串类型的加减乘除的运算方式有点相似,都应该从左往右依次读取字符,并做判断,如果符合要求就应该进行相应的动作。这里就可以运用到栈这个数据结构,先进后出依次判断。
思路:模拟堆栈,遇到左括号压入栈,遇到右括号取栈顶进行判断,查看是否匹配,匹配则正确,否则错误。
注意:临界情况,如果循环完后栈内仍然有左括号,则返回false;
class Solution {
public:
bool isValid(string s)
{
int len=s.length();
vector
myStack;
for(int i=0;i
0) return false;
return true;
}
};
最后
以上就是妩媚西装最近收集整理的关于Valid Parentheses的全部内容,更多相关Valid内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复