首页 > 试题广场 >

合法括号序列判断

[编程题]合法括号序列判断
  • 热度指数:14711 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

给定一个字符串A和其长度n,请返回一个bool值代表它是否为一个合法的括号串(只能由括号组成)。

测试样例:
"(()())",6
返回:true
测试样例:
"()a()()",7
返回:false
测试样例:
"()(()()",7
返回:false
头像 星不离月
发表于 2022-10-17 15:06:28
import java.util.*; public class Parenthesis {     public boolean chkParenthesis(String A,&nbs 展开全文
头像 vihem
发表于 2021-10-08 12:22:03
会不会有点啰嗦。。。 import java.util.*; public class Parenthesis { public boolean chkParenthesis(String A, int n) { if (A.length() != n) return fa 展开全文
头像 阿贝尔的日记
发表于 2022-09-21 13:14:07
合法括号判断 合法括号判断 /* 2022年09月21日 11:43:09 栈中存放左括号,当遇到右括号之后, 检查栈中是否有左括号,如果有则出栈,如果没有,则说明不匹配。 最后判断栈是否为空 */ class Parenthesis { public: bool chkParenthe 展开全文
头像 一个小小小小萌新
发表于 2021-07-30 21:16:36
做题的时候,在第三个测试用例上出了点小问题,然后发现自己没有加上循环结束后判断栈是否为空的条件,为空时才可以返回true。 import java.util.*; public class Parenthesis { public boolean chkParenthesis(String 展开全文
头像 wayToMaster
发表于 2022-01-13 19:58:35
-- coding:utf-8 -- class Parenthesis: def chkParenthesis(self, A, n): # write code here l = 0 for i in range(n): c = A[i] if c == '(': l+=1 elif c == 展开全文
头像 鹅鹅鹅鹅鹅孝子
发表于 2023-06-17 21:28:32
class Parenthesis { public: bool chkParenthesis(string A, int n) { // write code here stack<char> heap; for (int i = 展开全文
头像 请叫我孙爱情
发表于 2024-08-08 18:30:29
import java.util.*; public class Parenthesis { public boolean chkParenthesis(String A, int n) { // write code here Stack<Chara 展开全文
头像 Dfine
发表于 2025-07-05 16:02:22
#include <vector> class Parenthesis { public: bool chkParenthesis(string A, int n) { // write code here vector<char> 展开全文
头像 有趣的迪恩在看牛客
发表于 2023-08-22 14:08:41
import java.util.*; public class Parenthesis { public boolean chkParenthesis(String A, int n) { // write code here if(n % 2 != 0) 展开全文
头像 喜欢可抵岁月漫长
发表于 2023-06-29 21:18:31
import java.util.*; public class Parenthesis { public boolean chkParenthesis(String A, int n) { if(n%2!=0){ return false; //奇 展开全文