第八周leetcode题
来源:互联网 发布:吉利知豆d1续航 编辑:程序博客网 时间:2024/06/13 06:49
Description:
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.
题意一句概括就是括号匹配。可以用栈来完成这道题。顺序读取输入的字符串,遇到左半边括号时将其放入栈,遇到右半边括号时先检查栈是否为空,若为空则匹配失败,不为空时若栈顶括号与读到的右半边括号不匹配的话匹配失败,匹配的话则弹出栈顶括号。在读取完整个字符串后若栈为空则匹配成功。代码如下:
class Solution {
public:
bool isValid(string s) {
stack<char> a;
for(int i=0;i<s.size();i++)
{
if(s[i]=='('||s[i]=='{'||s[i]=='[') a.push(s[i]);
else if(a.empty()) return 0;
else
{
if(s[i]==')')
if(a.top()=='(')a.pop();
else return 0;
if(s[i]=='}')
if(a.top()=='{')a.pop();
else return 0;
if(s[i]==']')
if(a.top()=='[')a.pop();
else return 0;
}
}
if(a.empty())return 1;
else return 0;
}
};
- 第八周leetcode题
- leetcode-python 第八周
- leetcode-第八周
- 第八周LeetCode
- 第八周LeetCode
- [leetcode]第八周作业
- LeetCode题解 第八周
- leetcode第八周解题总结
- 第八周LeetCode算法题两道
- Leetcode 算法习题 第八周
- 第八周:[leetcode] 228. Summary Ranges
- 第八周:[leetCode] 91. Decode Ways
- 第八周:[Leetcode]5. Longest Palindromic Substring
- 第八周:[Leetcode]516. Longest Palindromic Subsequence
- 第八周:[Leetcode]300. Longest Increasing Subsequence
- leetcode【第八周】链表划分
- Leetcode算法课程第八周(补)
- LeetCode第八题, String to Integer (atoi)
- JSP:Table表格属性总结
- 【C语言】类型转换原则
- leetcode题解-105 && 106. Construct Binary Tree from Preorder (PostOrder) and Inorder Traversal
- Qt错误锦集--Cannot retrieve debugging output
- IOS端APP测试日志查看方法
- 第八周leetcode题
- java是通过java虚拟机进行编译和运行的。
- /etc/shadow和;/etc/passwd
- 二叉树的遍历
- 常见的dos命令
- POJ 1218 THE DRUNK JAILER
- 数据结构复习之快速排序的简单理解
- 动态规划练习一之最低通行费
- servlet基础--待更新