codility之Brackets

WBOY
Release: 2016-06-20 12:40:44
Original
1217 people have browsed it

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;    }
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!