当前位置:网站首页>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() 函数向数组尾部插入一个或多个元素。
边栏推荐
- 迁移学习——Discriminative Transfer Subspace Learning via Low-Rank and Sparse Representation
- Kubernetes第零篇:认识kubernetes
- 小程序毕设作品之微信体育馆预约小程序毕业设计成品(3)后台功能
- more grown, more lonely
- 小程序毕设作品之微信体育馆预约小程序毕业设计成品(1)开发概要
- 深度学习Course2第二周Optimization Algorithms习题整理
- seaborn笔记:可视化统计关系(散点图、折线图)
- 19 Lectures on Disassembly of Multi-merchant Mall System Functions - Invoice Management on the Platform
- SOM Network 1: Principles Explained
- 如何防范 DAO 中的治理攻击?
猜你喜欢
Recycling rental system 100% open source without encryption Mall + recycling + rental
xctf攻防世界 Web高手进阶区 web2
【开源】Sentinel高性能高可用集群限流解决方案
力扣第 304 场周赛复盘
迁移学习——Discriminative Transfer Subspace Learning via Low-Rank and Sparse Representation
C语言必杀技3行代码把运行速度提升4倍
seaborn笔记:可视化统计关系(散点图、折线图)
Shell programming conditional statement
shell specification and variables
【C语言实现】两种计算平均成绩题型,博主精心整理,值得一读
随机推荐
xctf attack and defense world web master advanced area webshell
2022-08-01 第八组 曹雨 泛型 枚举
ImportError: `save_weights` requires h5py. Problem solved
漫长的投资生涯
小程序毕设作品之微信美食菜谱小程序毕业设计成品(5)任务书
求解多元多次方程解的个数
No more rolls!After joining ByteDance for a week, he ran decisively.
小程序容器+自定义插件,可实现混合App快速开发
不卷了!入职字节跳动一周就果断跑了。
46.全排列
移动端人脸风格化技术的应用
C语言必杀技3行代码把运行速度提升4倍
(翻译)按钮的对比色引导用户操作的方式
自建 Prometheus 采集腾讯云容器服务监控数据最佳实践
Today's sleep quality record 74 points
Lecture 3: Several common table field data types in MySQL database
[Mobile Web] Mobile terminal adaptation
User Experience | How to Measure User Experience?
How to prevent governance attacks in DAOs?
AIDL communication