输入一个字符串,检查里面的左右括号是否匹配

bool pipeikuohao(const char* str,int length)
{
if (str == nullptr || length <= 1)
  return false;

int a = 0;
int left = 0;
int right = 0;
bool findfirstright = false;
while (a < length)
{
  if (str[a] == '(' || str[a] == ')')
  {
    if (str[a] == '(')
      ++left;
    else if (str[a] == ')' && left > right)
      ++right;
    else
      return false;
  } 

  ++a;
}
if (left != right)
  return false;
return true;
}

posted @ 2021-08-09 01:44  jiangcm  阅读(285)  评论(0编辑  收藏  举报