当前位置:网站首页>[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;
}
};
边栏推荐
- Qu Weihai, chairman and CEO of Xinyi interactive, adheres to mutual benefit and win-win results, and Qu Weihai promotes enterprise development
- Talking about the practice of software defect management
- 【Oracle SQL】计算同比与环比(列转行进行偏移)
- JS的调用方式与执行顺序
- Print linked list in reverse order
- H. Take the Elevator 贪心
- Workflow activiti5.13 learning notes (I)
- VRRP principle and basic commands
- [MySQL from introduction to proficiency] [advanced chapter] (VI) storage engine of MySQL tables, comparison between InnoDB and MyISAM
- Intelligent fire protection application based on fire GIS system
猜你喜欢

Kingbasees SQL language reference manual of Jincang database (8. Functions (XI))

YOLOv6:又快又准的目标检测框架开源啦

Acquisition of bidding information

招标信息获取

How to divide the disks under the devices and drives in win10 new computer

【Oracle SQL】计算同比与环比(列转行进行偏移)

Amd zen4 game God u reached 208mb cache within this year, which is unprecedented

Flex layout

Mysql45 talking about global lock, table lock and row lock

redis 哨兵集群搭建
随机推荐
Convolutional neural network (II) - deep convolutional network: case study
VRRP principle and basic commands
Huawei cloud koomessage is a new marketing weapon in the hot public beta
机械制造企业如何借助ERP系统,做好生产管理?
Kingbasees SQL language reference manual of Jincang database (10. Query and sub query)
ament_cmake生成ROS2库并链接
Redis sentinel cluster setup
How can programmers improve mental internal friction?
[Oracle SQL] calculate year-on-year and month on month (column to row offset)
flex布局
“子问题的递归处理”——判断两棵树是不是相同的树——以及 另一颗树的子树
【pytorch】CNN实战-花朵种类识别
二叉树的前中后序遍历——本质(每个节点都是“根”节点)
CV (1)- Introduction
Introduction to three feasible schemes of grammatical generalization
TPS Motion(CVPR2022)视频生成论文解读
Kingbasees SQL language reference manual of Jincang database (11. SQL statement: abort to alter index)
Unity2d animator cannot create transition
英语句式参考纯享版 - 状语从句
Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)