检查“()”是否匹配并返回深度

bool checkArr(char * arr, int len, int* max)
{
    if (NULL == arr || len == 0 || max == NULL)
    {
        return false;
    }

    int maxdepth = 0, deep = 0;
    for (int i = 0; i < len; i++)
    {
        char tmp = arr[i];
        if (tmp == '(')
        {
            deep++;
        }
        else if (tmp == ')')
        {
            deep--;
        }
        maxdepth = deep > maxdepth ? deep : maxdepth;
    }

    *max = maxdepth;
    if (deep == 0)
    {
        return true; //deep长度是0,表示匹配;
    }

    return false;
}

 

posted @ 2021-03-11 19:27  唯一诺  阅读(41)  评论(0)    收藏  举报