当前位置:网站首页>[day_070425] legal bracket sequence judgment
[day_070425] legal bracket sequence judgment
2022-07-26 06:12:00 【On the Bank of Anhe Bridge】
Legal bracket sequence judgment
Title source
Cattle from : Legal bracket sequence judgment
Title Description
Given a string A And its length n, Please return one bool The value represents whether it is a legal string of parentheses ( Can only consist of parentheses ).
Example 1
Input
“(()())”,6
return
true
Example 2
Input
“()a()()”,7
return
false
Example 3
Input
“()(()()”,7
return
false
Thought analysis
- According to the stack, in and out 、 Last in first out feature to save the sequence of parentheses
- Traversal string , When encountering the left parenthesis, perform the stack operation , The right bracket is used to stack , Finally, if the stack is empty, the sequence is legal
- If there are other characters when traversing the string , Or the current character is a right parenthesis but the stack is empty , Description sequence is illegal , Go straight back to false
Code display
class Parenthesis {
public:
bool chkParenthesis(string A, int n) {
stack<char> mystack;
for (int i = 0; i < n; i++)
{
if (A[i] == '(')
{
mystack.push('(');// Push
}
else if (A[i] == ')')
{
// When the current character is a right parenthesis , First determine whether there are brackets in the stack
if (mystack.empty())
{
return false;
}
mystack.pop();// Out of the stack
}
else
{
return false;
}
}
if (mystack.empty())
{
return true;
}
return false;
}
};
边栏推荐
- Jincang database kingbasees SQL language reference manual (5. Operators)
- Kingbasees SQL language reference manual of Jincang database (6. Expression)
- What is spark serialization for?
- 实习运维知识积累
- Webapi collation
- Implementation of PHP multitask second timer
- Can you make a JS to get the verification code?
- Interview difficulties: difficulties in implementing distributed session, this is enough!
- Embedded sharing collection 15
- YOLOv6:又快又准的目标检测框架开源啦
猜你喜欢

【Day04_0421】C语言选择题
![[the most complete and detailed] ten thousand words explanation: activiti workflow engine](/img/4c/2e43aef33c6ecd67d40730d78d29dc.png)
[the most complete and detailed] ten thousand words explanation: activiti workflow engine

The time complexity of two recursive entries in a recursive function
C language explanation series - comprehensive exercises, guessing numbers games

redis 哨兵集群搭建

卸载手机自带APP的操作步骤

Talking about the practice of software defect management

Age is a hard threshold! 42 years old, Tencent level 13, master of 985, looking for a job for three months, no company actually accepted!

Mysql45 talking about infrastructure: how is an SQL query executed?

【pytorch】图片增广
随机推荐
L. Link with Level Editor I dp
Can you make a JS to get the verification code?
Intelligent fire protection application based on fire GIS system
【Day_05 0422】统计回文
Introduction to three feasible schemes of grammatical generalization
VS中使用动态库
Servlet无法直接获取request请求中的JSON格式数据
[2023 Jerry technology approval test questions in advance] ~ questions and reference answers
【Day_05 0422】连续最大和
YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
Ganglia安装部署流程
The time complexity of two recursive entries in a recursive function
Etcd database source code analysis - cluster membership changes log
Knowledge precipitation I: what does an architect do? What problems have been solved
TPS Motion(CVPR2022)视频生成论文解读
Flex layout
Kingbasees SQL language reference manual of Jincang database (8. Function (10))
Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4
机械制造企业如何借助ERP系统,做好生产管理?
RNN循环神经网络