美文网首页
写个函数,判断下面扩号是否闭合,左右对称即为闭合: ((()))

写个函数,判断下面扩号是否闭合,左右对称即为闭合: ((()))

作者: 天天天向上 | 来源:发表于2018-05-15 15:13 被阅读0次

写个函数,判断下面扩号是否闭合,左右对称即为闭合: ((())),)(()),(()))),(((((()),(()()),()()

function checkStr(string $checkStr) {

    $checkStr = str_replace(',', '', $checkStr);

    $strCount = strlen($checkStr);

    if ($checkStr[0] == ')' || $checkStr[$strCount-1] == '(') {
        return false;
    }

    $count = 0;

    for ($i=0; $i < $strCount; $i++) { 
        if ($checkStr[$i] == '(') {
            $count += 1;
        } else {
            $count -= 1;
        }
    }
    //debug($count);
    return $count == 0 ;
}
function debug($input) {
    if (isset($_GET['_debug']) && $_GET['_debug']==1) {
        var_dump($input);
    }
}
var_dump(checkStr('((())),)(()),(()))),(((((()),(()()),()()'));
var_dump(checkStr('(((()))),()'));
var_dump(checkStr('(((())),()'));
var_dump(checkStr(')('));

相关文章

网友评论

      本文标题:写个函数,判断下面扩号是否闭合,左右对称即为闭合: ((()))

      本文链接:https://www.haomeiwen.com/subject/bazedftx.html