当前位置:网站首页>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() 函数向数组尾部插入一个或多个元素。
边栏推荐
- [Mobile Web] Mobile terminal adaptation
- The thing about npm
- 小程序毕设作品之微信美食菜谱小程序毕业设计成品(7)中期检查报告
- Flutter基础学习(一)Dart语言入门
- Postman 批量测试接口详细教程
- 高等代数_证明_矩阵的行列式为特征值之积, 矩阵的迹为特征值之和
- 易周金融分析 | 银行ATM机智能化改造提速;互联网贷款新规带来挑战
- Wechat Gymnasium Reservation Mini Program Graduation Design Finished Work Mini Program Graduation Design Finished Product (2) Mini Program Function
- Implementation principle of VGUgarbage collector (garbage collector)
- 【建议收藏】ヾ(^▽^*)))全网最全输入输出格式符整理
猜你喜欢

游戏元宇宙发展趋势展望分析

LeetCode952三部曲之一:解题思路和初级解法(137ms,超39%)

Prufer sequence

迁移学习——Discriminative Transfer Subspace Learning via Low-Rank and Sparse Representation

Today's sleep quality record 74 points

小程序容器+自定义插件,可实现混合App快速开发

No more rolls!After joining ByteDance for a week, he ran decisively.

scikit-learn no moudule named six

教你VSCode如何快速对齐代码、格式化代码

2022 版 MySQL 巅峰教程,收藏好,慢慢看
随机推荐
程序员必备的 “ 摸鱼神器 ” 来了 !
你居然不懂Bitmap和Drawable? 相关知识大扫盲
How to add a game character to a UE4 scene
RxJs SwitchMapTo 操作符之移花接木
【牛客刷题-SQL大厂面试真题】NO4.出行场景(某滴打车)
第一讲 测试知多少
Yizhou Financial Analysis | The intelligent transformation of bank ATM machines is accelerated; the new Internet loan regulations bring challenges
(*゚ヮ゚)*【精品C语言整理】*(゚ヮ゚*)女盆友缠着你让你教她写代码怎么办?安排,三万字博文带你走遍C语言,从此不再害怕编程
SOM Network 2: Implementation of the Code
感觉自己好傻
How to prevent governance attacks in DAOs?
线程池分析
解决 win10 下 ISE14.7的 iMPACT 崩溃问题 - FPGA 笔记
APP专项测试:流量测试
选择合适的 DevOps 工具,从理解 DevOps 开始
Unity Shader general lighting model code finishing
2022 edition of MySQL tutorial, top collection good, take your time
10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
Go 微服务开发框架DMicro的设计思路
使用Jenkins做持续集成,这个知识点必须要掌握