当前位置:网站首页>Niuke's brush question -- judgment of legal bracket sequence
Niuke's brush question -- judgment of legal bracket sequence
2022-06-11 18:29:00 【HHYX.】
Legal bracket sequence judgment
Topic link : Legal bracket sequence judgment
Title Description
describe
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 ).
The test sample :
Topic analysis
In the question is to judge whether the brackets are reasonable
There are several possibilities for unsuccessful matching :
- The current character is not a bracket , In this case, you can return directly false
- Incomplete bracket match , A single left or right parenthesis appears
Here you can use a stack to store strings , If the left bracket is encountered, it will be put on the stack , If the right bracket is encountered, judge whether there is a matching left bracket at the top of the stack , If so, the top element of the stack is pushed out of the stack , If not, the match is incomplete , More than the right half . If the last stack is empty , It means that there are matching parentheses . If the stack is not empty, it indicates that there are extra left parentheses
The code implementation is as follows :
Code implementation
bool chkParenthesis(string A, int n) {
// write code here
stack<char> cp;
for(size_t i=0;i<A.size();i++)
{
if(A[i]=='(')
{
cp.push(A[i]);
}
else if(A[i]==')')
{
if(!cp.empty() && cp.top()=='(')
{
cp.pop();// If it is a matching left parenthesis, it will be out of the stack
}
else
{
return false;
}
}
else
{
return false;
}
}
if(cp.empty())
{
return true;
}
else
{
return false;
}
}

边栏推荐
- 神经网络与深度学习-2- 机器学习简单示例-PyTorch
- SQL error injection 1
- 学习使用LSTM和IMDB评论数据进行情感分析训练
- [c language] output the students with the highest scores with a structure. There can be multiple highest scores
- Function and principle of key in V-for
- SISO Decoder for a General (n, N - 1) SPC Code (Supplementary section 3)
- 新项目 搭建环境方法
- [C语言]用结构体按分数高低降序输出学生的姓名和分数
- SISO decoder for min sum (supplementary Chapter 2)
- .net core redis hyperloglog类型
猜你喜欢

Sorted circular linked list

力扣23题,合并K个升序链表

Ti am64x - the latest 16nm processing platform, designed for industrial gateways and industrial robots

Introduction to social engineering practice

使用Visdom對損失函數進行監控

排序的循环链表

TI AM64x——最新16nm处理平台,专为工业网关、工业机器人而生

TR-069协议介绍

* Jetpack 笔记 Room 的使用

Quanzhi technology T3 development board (4-core arm cortex-a7) - mqtt communication protocol case
随机推荐
On the problem that the while loop condition in keil does not hold but cannot jump out
Feign shares login information for request
[c language] shift elements after sorting elements of an array
SA token single sign on SSO mode 2 URL redirection propagation session example
力扣23题,合并K个升序链表
Quanzhi technology T3 development board (4-core arm cortex-a7) - video development case
MATLAB 保存imshow绘制图片到指定文件夹中的两种方法
SISO Decoder for a General (n, N - 1) SPC Code (Supplementary section 3)
“LSTM之父”新作:一种新方法,迈向自我修正的神经网络
使用Visdom對損失函數進行監控
LDAP 目录服务器的现代化应用
VIM common commands
神经网络与深度学习-2- 机器学习简单示例-PyTorch
TR-069 protocol introduction
Async leads to unexpected function results and changes the intention of the original code; await is only valid in async functions and the top level bodies of modules
高并发架构设计
v-for循环遍历
264 Concepts
Learn to use LSTM and IMDB comment data for emotion analysis training
[Golang]力扣Leetcode - 292. Nim 游戏(数学)