增加(0020.有效的括号.md):PHP版本
This commit is contained in:
parent
0e3a1bc3dc
commit
156d8fdd92
|
|
@ -401,5 +401,33 @@ bool isValid(char * s){
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
PHP:
|
||||||
|
```php
|
||||||
|
// https://www.php.net/manual/zh/class.splstack.php
|
||||||
|
class Solution
|
||||||
|
{
|
||||||
|
function isValid($s){
|
||||||
|
$stack = new SplStack();
|
||||||
|
for ($i = 0; $i < strlen($s); $i++) {
|
||||||
|
if ($s[$i] == "(") {
|
||||||
|
$stack->push(')');
|
||||||
|
} else if ($s[$i] == "{") {
|
||||||
|
$stack->push('}');
|
||||||
|
} else if ($s[$i] == "[") {
|
||||||
|
$stack->push(']');
|
||||||
|
// 2、遍历匹配过程中,发现栈内没有要匹配的字符 return false
|
||||||
|
// 3、遍历匹配过程中,栈已为空,没有匹配的字符了,说明右括号没有找到对应的左括号 return false
|
||||||
|
} else if ($stack->isEmpty() || $stack->top() != $s[$i]) {
|
||||||
|
return false;
|
||||||
|
} else {//$stack->top() == $s[$i]
|
||||||
|
$stack->pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 1、遍历完,但是栈不为空,说明有相应的括号没有被匹配,return false
|
||||||
|
return $stack->isEmpty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue