当前位置:网站首页>PHP算法之有效的括号
PHP算法之有效的括号
2022-08-01 22:08:00 【phpstory】
function isValid($s) {
$length = strlen($s);
if ($length <= 1) {
return false;
}
$tmp_arr = [")" => "(", "}" => "{", "]" => "["];
$arr = [];
for ($i=0; $i<$length; $i++) {
if (isset($tmp_arr[$s[$i]]) && $tmp_arr[$s[$i]] == end($arr)) {
array_pop($arr);
} else {
array_push($arr, $s[$i]);
}
}
return empty($arr) ? true : false;
}
var_dump($this->isValid("{[{]}"));
备注:
1、end() 函数将内部指针指向数组中的最后一个元素,并输出
2、array_pop() 函数删除数组中的最后一个元素。
3、array_push() 函数向数组尾部插入一个或多个元素。
边栏推荐
- 2022 版 MySQL 巅峰教程,收藏好,慢慢看
- Homework 8.1 Orphans and Zombies
- String - Trie
- 使用 Zokrates 在 BSV 上创建您的第一个 zkSNARK 证明
- Go 微服务开发框架DMicro的设计思路
- 编曲软件FL studio20.8中文版功能和作用
- Implementation principle of VGUgarbage collector (garbage collector)
- Still struggling with reporting tool selection?To take a look at this
- Prufer序列
- 【C语言实现】最大公约数的3种求法
猜你喜欢
随机推荐
使用 Zokrates 在 BSV 上创建您的第一个 zkSNARK 证明
数据分析面试手册《指标篇》
入门数据库Days4
2022-08-01 第八组 曹雨 泛型 枚举
Homework 8.1 Orphans and Zombies
Small program -- subcontracting
[深入研究4G/5G/6G专题-48]: 5G Link Adaption链路自适应-4-下行链路自适应DLLA-PDCCH信道
网络水军第一课:手写自动弹幕
Today's sleep quality record 74 points
Raspberry Pi information display small screen, display time, IP address, CPU information, memory information (C language), four-wire i2c communication, 0.96-inch oled screen
KMP 字符串匹配问题
模拟数据之mockjs
今年的很美味
MySQL related knowledge
Implementation principle of VGUgarbage collector (garbage collector)
找工作必备!如何让面试官对你刮目相看,建议收藏尝试!!
如何理解 new (...args: any[]) => any
SOM网络2: 代码的实现
第一讲 测试知多少
高等代数_证明_矩阵的行列式为特征值之积, 矩阵的迹为特征值之和









