当前位置:网站首页>20. valid brackets
20. valid brackets
2022-06-11 08:56:00 【Drag is me】
leetcode Force button to brush questions and punch in
subject :20. Valid parenthesis
describe : Given one only includes ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ String s , Determines whether the string is valid .
Valid string needs to meet :
Opening parentheses must be closed with closing parentheses of the same type .
The left parenthesis must be closed in the correct order .
Their thinking
1、 Parentheses encountered , It is basically a stack solution ;
2、 The stack is empty. , direct push Come in ; Stack is not empty. , Compare whether the stack top element and the new element are paired , Pairing is directly out of the stack ;
3、 Finally, judge whether the stack is empty , All are paired. The stack must be empty , return true, Instead, return to false;
Source code ##
class Solution {
public:
bool ispair(char a, char b) {
switch (a) {
case '(':
if (b == ')') return true;
else return false;
case '{':
if (b == '}') return true;
else return false;
case '[':
if (b == ']') return true;
else return false;
default:
return false;
}
}
bool isValid(string s) {
stack<char>sc;
for (int i = 0; i < s.size(); ++i) {
if (sc.empty()) {
sc.push(s[i]);
continue;
} else {
char temp = sc.top();
if (ispair(temp, s[i])) sc.pop();
else sc.push(s[i]);
}
}
if (sc.empty()) return true;
else return false;
}
};
边栏推荐
- SAP ABAP data types and data objects
- leetcode - 739. 每日温度
- EN45545-2 R26垂直燃烧测试介绍
- SAP 物料主数据归档
- (2) Analysis of AAC source code from the perspective of architecture design - my livedata
- multiplication table
- Codetop - sort odd ascending even descending linked list
- EN 45545-2:2020 T11烟毒性检测
- vagrant 安装踩坑
- 2、 Rendering camera data
猜你喜欢

领导让我重写测试代码,我也要照办嘛?

C语言打印菱形

哪些Apple设备支持这次系统更新?来看你的旧Apple设备支持最新系统吗

Hibernate L2 cache

Screening frog log file analyzer Chinese version installation tutorial

TextView文本大小自动适配与TextView边距的去除

Matlab学习8-图像处理之线性与非线性锐化滤波、非线性平滑滤波

Matlab learning 8- linear and nonlinear sharpening filtering and nonlinear smoothing filtering of image processing

Android 面试笔录(精心整理篇)

Sword finger offer 62 The last remaining number in the circle
随机推荐
[node] NPM part
木板ISO 5660-1 热量释放速率摸底测试
M1 chip guide: M1, M1 pro, M1 Max and M1 ultra
win10家庭版如何连接远程桌面
Sword finger offer 10- ii Frog jumping on steps
驱动壳美国测试UL 2043 符合要求有哪些?
【237. 删除链表中的节点】
844. 比较含退格的字符串
Can not connect to local MySQL server through socket ‘/tmp/mysql. sock (2)‘
光伏板怎么申请ASTM E108阻燃测试?
What is the process of en 1101 flammability test for curtains?
En45545-2 R26 vertical combustion test introduction
EN 45545 R24氧指数测试方法解析
86. 分隔链表
欧洲家具EN 597-1 跟EN 597-2两个阻燃标准一样吗?
Hibernate L2 cache
File system check of the root filesystem failed
PHP解决中文显示乱码
SAP ABAP field symbol
Getting started with Zipkin