php使用栈的思路去操作
<?php class Solution { /** * @param String $s * @return Boolean */ function isValid($s) { $arr = array( ")" => "(", "]" => "[", "}" => "{" ); $count = strlen($s); $stack = array(); if($count %2 != 0) return false; for ($i = 0; $i < $count; $i++) { if(isset($arr[$s[$i]])) { if(count($stack) == 0) { return false ; } $tmp = array_pop($stack); if($tmp != $arr[$s[$i]]) { return false; } } else { $stack[] = $s[$i]; } } //var_dump($stack); return $stack ? false : true; } }