当前位置:网站首页>力扣 20. 有效的括号
力扣 20. 有效的括号
2022-06-10 16:34:00 【呦,又写BUG呢】
题目描述
给定一个只包括(、)、[、]、{ 、}的字符串s,判断字符串是否有效。
有效字符串需满足:
- 左括号必须用相同类型的右括号闭合
- 左括号必须以正确的顺序闭合
示例 1:
输入:s = "()"
输出:true
示例 2:
输入:s = "()[]{}"
输出:true
示例 3:
输入:s = "(]"
输出:false
示例 4:
输入:s = "([)]"
输出:false
示例 5:
输入:s = "{[]}"
输出:true
题解
C++
采用栈处理,时间复杂度 O ( n ) O(n) O(n),空间复杂度取决于暂时无法匹配的括号的最大数目:
#include <iostream>
#include <stack>
using namespace std;
class Solution {
public:
static bool isValid(string s) {
stack<char> brackets_stack;
for (char c: s) {
if (c == '(' || c == '[' || c == '{') {
brackets_stack.push(c);
} else if (brackets_stack.empty()) {
return false;
} else if (c == ')') {
if (brackets_stack.top() == '(') {
brackets_stack.pop();
} else {
return false;
}
} else if (c == ']') {
if (brackets_stack.top() == '[') {
brackets_stack.pop();
} else {
return false;
}
} else if (c == '}') {
if (brackets_stack.top() == '{') {
brackets_stack.pop();
} else {
return false;
}
}
}
return brackets_stack.empty();
}
};
int main() {
cout << Solution::isValid("]") << endl;
return 0;
}
在扫描到左括号的时候,可以直接入栈右括号,这样在扫描到右括号时就只需要和栈顶相比较就可以了,可以简化代码。
#include <iostream>
#include <stack>
using namespace std;
class Solution {
public:
static bool isValid(string s) {
stack<char> brackets_stack;
for (char c: s) {
if (c == '(') {
brackets_stack.push(')');
} else if (c == '[') {
brackets_stack.push(']');
} else if (c == '{') {
brackets_stack.push('}');
} else if (brackets_stack.empty() || brackets_stack.top() != c) {
return false; // 栈空说明有右括号无法被匹配 栈顶不同说明括号不匹配
} else {
brackets_stack.pop(); // 匹配成功将栈顶出栈
}
}
return brackets_stack.empty();
}
};
int main() {
cout << Solution::isValid("]") << endl;
return 0;
}
边栏推荐
- 李飞飞:我更像物理学界的科学家,而不是工程师|深度学习崛起十年
- Nat. Commun. | Knowledge integration and decision support for accelerating the discovery of antibiotic resistance genes
- SOA architecture / test phase interface description language transformation scheme
- Full link tracking & performance monitoring tool skywalking practice
- [BSP video tutorial] BSP video tutorial issue 17: single chip microcomputer bootloader topic, startup, jump configuration and various usage of debugging and downloading (2022-06-10)
- ADB is not an internal or external command, nor is it a runnable program or batch file
- 当v-if和v-for需要同时使用的时候
- HTML+PHP+Mysql登录注册页面
- 软件项目管理 6.10.成本预算
- 域名备案和icp备案有哪些区别?
猜你喜欢

软件项目管理 6.10.成本预算

Cannot locate a 64-bit Oracle Client library:The specified module could not be found.

2022年上海市安全员C证操作证考试题库模拟考试平台操作
![[BSP video tutorial] BSP video tutorial issue 17: single chip microcomputer bootloader topic, startup, jump configuration and various usage of debugging and downloading (2022-06-10)](/img/75/a3336aa7314a2dfc9a7a32995793e7.png)
[BSP video tutorial] BSP video tutorial issue 17: single chip microcomputer bootloader topic, startup, jump configuration and various usage of debugging and downloading (2022-06-10)

PyTorch基础(一)-- Anaconda 和 PyTorch安装

Internet enterprises and chips

重庆第一个科创板IPO,来了

2022G1工业锅炉司炉考题及在线模拟考试

C#_串口通信项目
Example code of PHP for uploading multiple pictures
随机推荐
期货账户资金安全吗?
AIChE | 集成数学规划方法和深度学习模型的从头药物设计框架
Daily question -1287 Elements that appear more than 25% in an ordered array
Facebook AI | 从数百万预测结构中学习逆向折叠
Swift 3pThread tool Promise Pipeline Master/Slave Serial Thread confinement Serial queue
新思科技助力以色列Visuality Systems推进安全“左移”
Detailed derivation of perspective projection transformation and related applications
善用产业链招商,打造产业集群效应,实现产业协同发展
丢失的遗传力--Missing heritability
品牌难立,IPO难行,中国茶企困于“传统”?
See how advanced technology changes human life
ahk函数命令大全
Fabric.js 缩放画布
目标客户匹配数据表格成功案例展示
What should be done to improve the service level of the park and optimize the business environment
2022年上海市安全员C证操作证考试题库模拟考试平台操作
Fabric. JS centered element
2022年焊工(初级)试题模拟考试平台操作
Chongqing's first sci tech Innovation Board IPO is coming
Station B doesn't want to be a "conscience aiyouteng"