当前位置:网站首页>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() 函数向数组尾部插入一个或多个元素。
边栏推荐
猜你喜欢

Kubernetes第零篇:认识kubernetes

编曲软件FL studio20.8中文版功能和作用

高等代数_证明_矩阵的行列式为特征值之积, 矩阵的迹为特征值之和

19 Lectures on Disassembly of Multi-merchant Mall System Functions - Invoice Management on the Platform

不卷了!入职字节跳动一周就果断跑了。

Ten years after graduation, financial freedom: those things that are more important than hard work, no one will ever teach you

(翻译)按钮的对比色引导用户操作的方式

10 Practical Uses of NFTs (NFT System Development)

小程序毕设作品之微信体育馆预约小程序毕业设计成品(3)后台功能

03、GO语言变量定义、函数
随机推荐
深度学习Course2第二周Optimization Algorithms习题整理
(Translation) How the contrasting color of the button guides the user's actions
Analysis of the development trend of game metaverse
Go 微服务开发框架DMicro的设计思路
shell specification and variables
LeetCode952三部曲之一:解题思路和初级解法(137ms,超39%)
MySQL related knowledge
不卷了!入职字节跳动一周就果断跑了。
漫长的投资生涯
Graph Theory - Strongly Connected Component Condensation + Topological Sort
User Experience | How to Measure User Experience?
程序员必备的 “ 摸鱼神器 ” 来了 !
基于 OData 模型和 JSON 模型的 SAP UI5 表格控件行项目的添加和删除实现
Wechat Gymnasium Reservation Mini Program Graduation Design Finished Work Mini Program Graduation Design Finished Product (2) Mini Program Function
The must-have "fishing artifact" for programmers is here!
【移动Web】移动端适配
工程建筑行业数据中台指标分析
用户体验 | 如何度量用户体验?
Centos7--MySQL的安装
熟悉的朋友