leetcode - 20.有效的括号(LinkedHashMap)

leetcode题目有效的括号,分类是easy,但是博主前前后后提交了几十次才通过,现在记录一下使用Java语言的写法。
在这里插入图片描述

题目链接: 20.有效的括号

题目描述:

给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。有效字符串需满足:
1.左括号必须用相同类型的右括号闭合。
2.左括号必须以正确的顺序闭合。
3.每个右括号都有一个对应的相同类型的左括号。

题目示例:

示例 1:
输入:s = "()"
输出:true示例 2:
输入:s = "()[]{}"
输出:true示例 3:
输入:s = "(]"
输出:false

题目提示:

1. <= s.length <= 104
2. 仅由括号 '()[]{}' 组成

题目解读:

每个左括号都要有成对的右括号,成对的含义是
(1)属于"()"、"[]"、"{}"中的任意一对
(2)中间不存在其他字符,比如"()"
(3)中间存在其他成对的字符,比如"({[]})"

示例图:
在这里插入图片描述

一、 递归

基于以上的理解,博主最初选择使用递归消除字符的做法。只要左右相邻的字符成对就将其消除,递归至LinkedHashMap的长度为0或不再出现可消除的字符。

思路:
(1)使用LinkedHashMap保存入参用例中的所有字符。
(2)遍历LinkedHashMap,在存在相邻两个元素符合成对匹配条件时,将这两个元素记录下来,在一次遍历结束后从LinkedHashMap中移除。
(3)当LinkedHashMap长度为0,或者在一次遍历中没有产生新的待删除的元素,则跳出循环。
(4)当最终LinkedHashMap长度为0时,说明全部匹配成功。
class Solution {public boolean isValid(String s) {boolean res = false;char[] charArray = s.toCharArray();if (charArray.length % 2==1) {return res;}HashMap<Integer, String> linkedHashMap = new LinkedHashMap<>();for (int i =0; i < charArray.length; i++) {linkedHashMap.put(i, String.valueOf(charArray[i]));}List<String> leftList = Arrays.asList("(", "[", "{");List<String> rightList = Arrays.asList(")", "]", "}");// 当map中全部消除完,或上一轮没有可消除的值时跳出循环int lastIndex = 0;List<Integer> removeKeys = new ArrayList<>();while (!linkedHashMap.isEmpty()) {removeKeys = new ArrayList<>();for (Integer key : linkedHashMap.keySet()) {int pos = new ArrayList<Integer>(linkedHashMap.keySet()).indexOf(key);// 从第二个开始跟前面的比较,前面取最新的linkedMap的第一个,不是key=0if (pos > 0) {if (leftList.contains(linkedHashMap.get(lastIndex))&& rightList.contains(linkedHashMap.get(key))&& (leftList.indexOf(linkedHashMap.get(lastIndex)) == rightList.indexOf(linkedHashMap.get(key)))) {// 删除对应位置removeKeys.add(lastIndex);removeKeys.add(key);linkedHashMap.put(lastIndex, "0");linkedHashMap.put(key, "0");}}// 保存上一个key的值lastIndex = key;}if (!removeKeys.isEmpty()) {removeKeys.forEach(linkedHashMap::remove);} else {break;}}if (linkedHashMap.isEmpty()) {res = true;}return res;}}

该做法可以计算正确,但在遇到以下测试用例时,惨遭Time Limit Exceeded 👇。

String s = "[([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([([()])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])])]";

该测试用例长度为7000,以递归消除元素的方法需要递归遍历几千次,速度非常的慢。那么,提高速度最好的办法就是减少遍历次数。

二、栈

在评论区看了一圈,发现大佬们的基本都是用栈来解决。
但是博主平时基本不使用栈这种数据结构,对栈的了解仅停留在以下层面。

栈(Stack)是一种常见的数据结构,具有后进先出(LIFO,Last In First Out)的特性,即最后入栈的元素最先出栈。

所以依然选择使用熟悉的LinkedHashMap去实现LIFO这一行为。在加入LinkedHashMap前先判断是否与末尾的元素匹配,再决定后续的操作。仅需要遍历一次就可以完成所有的匹配判断。

思路:
(1)遍历入参的字符数组。
(2)如果linkedMap长度为空,当前字符直接加入,作为第一个字符。
(3)如果linkedMap长度不为空,取末尾元素与当前元素进行匹配。匹配成功,将末尾元素消除(弹出);匹配失败,将当前元素加入linkedMap(压入)
(4)字符数组遍历结束后,如果linkedMap为空,说明全部匹配。
class Solution {public boolean isValid(String s) {boolean res = false;char[] charArray = s.toCharArray();if (charArray.length % 2==1) {return res;}List<String> leftList = Arrays.asList("(", "[", "{");List<String> rightList = Arrays.asList(")", "]", "}");// 栈:一个一个往堆栈里填入,如果跟前一个匹配就双双丢出HashMap<Integer, String> linkedHashMap = new LinkedHashMap<>();for (int i =0; i < charArray.length; i++) {String s1 = String.valueOf(charArray[i]);// 当前linkedMap中的排列的第一位if (i== 0 || linkedHashMap.isEmpty()) {if (rightList.contains(s1)) {return false;}linkedHashMap.put(i, String.valueOf(charArray[i]));} else {int lastPos = new ArrayList<Integer>(linkedHashMap.keySet()).get(linkedHashMap.size()-1);// 判断跟linkedMap中最新一位元素是否匹配String lastS1 = linkedHashMap.get(lastPos);if (leftList.contains(lastS1)&& rightList.contains(s1)&& (leftList.indexOf(lastS1) == rightList.indexOf(s1))) {// 匹配成功,不加入linkedMap,并且将前一个元素从map中移除(弹出)linkedHashMap.remove(lastPos);} else {// 不匹配,将该元素加入linkedMap中(压入)linkedHashMap.put(i, String.valueOf(charArray[i]));}}}// 全部消除完毕才是通过if (linkedHashMap.isEmpty()) {res = true;}return res;}
}

不过在使用LinkedHashMap时也碰到了一些问题,因为元素可以随意移除,所以key的排序不是连续的12345等。需要将key转为ArrayList,再通过ArrayList.get()方法获取最新一位元素key。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/20478.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【漏洞复现】WordPress Country State City Dropdown CF7插件 SQL注入漏洞(CVE-2024-3495)

0x01 产品简介 Country State City Dropdown CF7插件是一个功能强大、易于使用的 WordPress 插件&#xff0c;它为用户在联系表单中提供国家.州/省和城市的三级下拉菜单功能&#xff0c;帮助用户更准确地填写地区信息。同时&#xff0c;插件的团队和支持也非常出色&#xff0c…

香橙派 Kunpeng Pro使用教程:从零开始打造个人私密博客

一、引言 在这个日益互联的世界中&#xff0c;单板计算机已经成为创新和个性化解决方案的重要载体。而在单板计算机领域&#xff0c;香橙派 Kunpeng Pro凭借其强大的性能和灵活的应用潜力&#xff0c;正逐渐吸引着全球开发者和技术爱好者的目光。 作为一款集成了华为的鲲鹏处…

单调栈原理+练习

首先用一道题引出单调栈 码蹄集 (matiji.net) 首先画一个图演示山的情况&#xff1a; 最暴力的做法自然是O(n方)的双循环遍历&#xff0c;这么做的思想是求出当前山右侧有多少座比它小的山&#xff0c;遇见第一个高度大于等于它的就停止。 但是对于我们所求的答案数&#xff…

codefun的蓝桥杯国赛之旅

前言 好久没有刷算法了&#xff0c;今天完成了我的蓝桥杯国赛之旅&#xff01; 总的来说&#xff0c;比赛的过程不是很顺利&#xff0c;只能ac两道题目&#xff0c;好多题都是有思路&#xff0c;但是要么是写不出来&#xff0c;要么是debug不出来&#xff0c;多重背包&#xf…

Docker(Centos7+)

先确定是否 Centos 7 及以上的版本 查看是否 ping 通外网 linux centos7运行下面的代码&#xff0c;基本上都可以正常安装 # 删除之前的docker残留 yum -y remove docker*yum install -y yum-utilsyum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/…

Controller类明明写了@CrossOrigin跨域注解,但还是有跨域问题

可能是写的过滤器干扰到了跨域处理。如&#xff1a; 此时&#xff0c;先注释掉过滤器注解&#xff0c;让其不生效&#xff0c;就可以避免干扰跨域处理了 不过&#xff0c;这只能暂时解决该问题&#xff0c;毕竟过滤器还是要用的&#xff0c;后续我再探索一下。。。。。。。

[个人笔记] 记录docker-compose使用和Harbor的部署过程

容器技术 第三章 记录docker-compose使用和Harbor的部署过程 容器技术记录docker-compose使用和Harbor的部署过程Harborhttps方式部署&#xff1a;测试环境部署使用自签名SSL证书https方式部署&#xff1a;正式环境部署使用企业颁发的SSL证书给Docker守护进程添加Harbor的SSL证…

ctfshow unserialize

开始反序列化 web255 是从cookie中unserialize得到实例&#xff0c;考虑修改cookie中键user的值 $result urlencode(serialize(new ctfShowUser())); 为何需要url编码呢&#xff0c;不url编码也能成。url编码是对称加密&#xff0c;编码也不影响 web256 考察!不完全等于&…

Ollama+OpenWebUI+Phi3本地大模型入门

文章目录 OllamaOpenWebUIPhi3本地大模型入门一、基础环境二、Ollama三、OpenWebUI Phi3 OllamaOpenWebUIPhi3本地大模型入门 完全不懂大模型的请绕道&#xff0c;相信我李一舟的课程比较适合 Ollama提供大模型运行环境&#xff0c;OpenWebUI提供UI&#xff0c;Phi3就是那个大…

【学习】自动化测试与单元测试框架的差异化解析

在软件开发的世界中&#xff0c;质量保证是构建可靠、健壮应用程序的关键一环。在这个过程中&#xff0c;自动化测试和单元测试框架是确保代码质量的两种重要工具。尽管它们在目标上有着共同点——提高软件测试的效率和有效性&#xff0c;但它们在应用场景、功能特点以及实现方…

Vue基础知识:vue3 对于 ref 值的类型定义

vue3 对于 ref 值的类型定义 从 vue2 到 vue3 之后&#xff0c;所有变量值都需要添加 ref() const name ref(kyle)意思是 vue3 变量由 vue 来控制&#xff0c;上面的 name 变量值并不是原始的字符串 kyle 而是被套了一层类型为 Ref 的对象。 使用的时候 console.log(name.v…

Day45 动态规划part05

LC1049最后一块石头重量II(未掌握) 未掌握分析&#xff1a;其实本题跟LC416分割等和子集类似&#xff0c;本质上题目的要求是尽量让石头分成重量相同的两堆&#xff0c;相撞之后剩下的石头最小&#xff0c;也就是01背包问题weight和value都是stones数组&#xff0c;题目可以看…

深度学习-02-创建变量的函数

深度学习-02-创建变量的函数 本文是《深度学习入门2-自製框架》 的学习笔记&#xff0c;记录自己学习心得&#xff0c;以及对重点知识的理解。如果内容对你有帮助&#xff0c;请支持正版&#xff0c;去购买正版书籍&#xff0c;支持正版书籍不仅是尊重作者的辛勤劳动&#xff0…

Go-Admin后台管理系统源码(GO+VUE)编译与部署

1.克隆源码: # Get backend code git clone https://github.com/go-admin-team/go-admin.git# Get the front-end code git clone https://github.com/go-admin-team/go-admin-ui.git3.下载并安装GO开发环境: 3.编译管理后台后端 # Enter the go-admin backend project cd ./…

Pytorch索引、切片、连接

文章目录 1.torch.cat()2.torch.column_stack()3.torch.gather()4.torch.hstack()5.torch.vstack()6.torch.index_select()7.torch.masked_select()8.torch.reshape9.torch.stack()10.torch.where()11.torch.tile()12.torch.take()13.torch.scatter() 1.torch.cat() torch.cat(…

基于排队理论的客户结账等待时间MATLAB模拟仿真

目录 1.程序功能描述 2.测试软件版本以及运行结果展示 3.核心程序 4.本算法原理 4.1 排队系统的组成 4.2 基本概念 4.3 常见的排队模型 5.完整程序 1.程序功能描述 基于排队理论的客户结账等待时间MATLAB模拟仿真&#xff0c;分析平均队长&#xff0c;平均等待时长&…

智慧交通视频AI监控识别解决方案

背景分析 随着社会的进步和科技的不断发展&#xff0c;互联网技术和AI视觉分析技术日益成熟&#xff0c;为传统交通监控领域带来了新的发展机遇。AI视觉分析技术的引入&#xff0c;不仅提升了交通监控的智能化和自动化水平&#xff0c;还显著减轻了交管部门的工作负担&#xf…

雷卯解析AECQ101与AECQ200

AEC&#xff08;汽车电子委员会&#xff09;推出了AECQ101和AECQ200这两项行业标准&#xff0c;作为汽车电子元件的“品质通行证”。上海雷卯已率先申请AECQ101证书。 鉴于有些客户不清楚AECQ101和AECQ200的区别&#xff0c;哪些供应商应该提供什么类别证书。本文将带您解析这…

本地知识库开源框架Fastgpt、MaxKB产品体验

本地知识库开源框架Fastgpt、MaxKB产品体验 背景fastgpt简介知识库共享部署 MaxKB总结 背景 上一篇体验了Quivr、QAnything两个开源知识库模型框架&#xff0c;这次介绍两款小众但是体验比较好的产品。 fastgpt 简介 FastGPT 是一个基于 LLM 大语言模型的知识库问答系统&am…

第四范式Q1业务进展:驰而不息 用科技锻造不朽价值

5月28日&#xff0c;第四范式发布今年前三个月的核心业务进展&#xff0c;公司坚持科技创新&#xff0c;业务稳步拓展&#xff0c;用人工智能为千行万业贡献价值。 今年前三个月&#xff0c;公司总收入人民币8.3亿元&#xff0c;同比增长28.5%&#xff0c;毛利润人民币3.4亿元&…