Home  >  Article  >  Backend Development  >  codility之Brackets

codility之Brackets

WBOY
WBOYOriginal
2016-06-20 12:40:441253browse

Brackets

题目链接:

Brackets

题目解析

其实就是平衡符号。

如何解决?

利用栈就可以搞定, 具体不明白的看书吧。

talk is cheap, show me the code~

代码:

function solution($S) {    // write your code in PHP5.5    $open_symb  = array( '{', '[', '(' );    $close_symb = array( '}', ']', ')' );    $check = array();    for($i = 0; $i< strlen($S); $i++) {        $v = $S[$i];        if (in_array($v, array_values($open_symb))) {            array_push($check, $v);        } elseif (in_array($v, array_values($close_symb))) {            $symbol = array_pop($check);            $cor_symbol = array_search($v, $close_symb);            if ($symbol !== $open_symb[$cor_symbol]) {                return  0;            }        }    }        if (!empty($check)) {        return 0;    }
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn