Leetcode20-Valid Parenthesis

leetcode 678. Valid Parenthesis String | 678. 有效的括号字符串(带缓存的暴力递归) 题目 https://leetcode.com/problems/valid-parenthesis-string/ 题解 带缓存的暴力递归,非常挫。用一个 string 模拟 stack,方便缓存记录状态。 class Solution { public boolean checkValidString(String s) { StringBuilder stack = new StringBuilder(); Map<String, Boolean> 阅读详情
给定一个字符串,只包含 ( , [ , { , ) , ] , } ,判定字符串中的括号匹配是否合法。
如 ”()” , “()[]{}” 是合法的

如 “(]”,”([)]” 是非法的 


思路:使用栈解决

遍历字符串

如果是左括号,就入栈

否则,判断栈顶元素和当前元素是否匹配


代码:

class Solution {
	public static boolean isValid(String s){

		Stack stack = new Stack();
		//遍历字符串
		for(int i = 0; i < s.length();i++){
		//如果是左括号入栈
			if(s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{' ){
				stack.push(s.charAt(i));
			}else{//否则,是右括号的话

				//1. 栈如果为空,返回false
				if(stack.isEmpty()){
					return false;
				}
				//2. 弹出栈顶元素,看和右括号s.charAt(i)是否匹配,不匹配的话就返回false,可以用一个临时变量
				char c = (Character) stack.pop();
	
				char match = ' ';
				if(s.charAt(i) == ')')
					match = '(';
				if(s.charAt(i) == ']')
					match = '[';
				if(s.charAt(i) == '}')
					match = '{';
	
				if(match != c){
					return false;
				}
			}
		}
		//遍历结束,栈不为空的话,返回false
		if(!stack.isEmpty()){
			return false;
		}
		//返回true
		return true;
	}
}


机械臂速成小指南(八):标准DH法实战——从连杆坐标系到运动学方程 本文详细介绍了机械臂运动学建模中的标准DH法,从连杆坐标系的建立到运动学方程的求解,提供了实用的操作步骤和代码示例。通过DH参数表和变换矩阵的构建,帮助读者快速掌握机械臂运动学建模的核心技术,并解决实际应用中的常见问题。 阅读详情

相关推荐

磁盘I/O %util特别高

磁盘I/O %util特别高

eagle89的专栏 3100

LeetCode-20 valid parenthesis

题目:括号匹配问题Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all v...

小零件儿的博客 287

C语言的文件读取与写入操作

学了一年的C语言了,现在回过头来做一下总结。并且博客开了挺长一段时间却没有谢什么实际的东西。现在做下总结,对之后的学习帮助应该挺大的。与大家共勉! 现在,我先来介绍一下C语言的文件读取与写入的原理和具体操作方法。 C语言文件读取与写入是通过将文件看成一个字符序列进行读入和写出的。所以读取与写入文件就用到了指针,而这个指针是一个特殊的指针,我们称为文件指针。 指针名称是

wzyxdwll的博客 3万+

Leetcode Valid Parentheses

[code]public class Solution { public boolean isValid(String s) { if(s==null || s.length()==0)return true; Stack<Character> stack=new Stack<Character>(); HashMap <Character,

311

LLC谐振电路增益公式推导

LLC谐振电路增益公式理解

金凯*博客 1万+

Java网页访问报错?手把手教你解决JRE插件缺失和浏览器拦截问题(附详细截图)

本文针对Java网页访问时常见的JRE插件缺失和浏览器拦截问题,提供了详细的诊断与解决方案。文章首先解释了问题根源在于现代浏览器出于安全考虑停止了对Java插件的支持,然后手把手指导用户确认Java安装、在控制面板配置安全站点列表,并针对Chrome、Edge、Firefox等主流浏览器的不同拦截机制给出了具体的应对策略,最后提供了系统性的故障排查清单,帮助用户彻底解决访问障碍。

weixin_29233857的博客 229

LeetCode - Valid Parenthesis

https://leetcode.com/problems/valid-parentheses/ Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must cl

chilakaka的专栏 461

ardupilot 日志分析《xKF1信息》

ardupilot 日志分析系列将会把ardupilot所有的日志文档进行分析,为我们分析无人机的运行状态,炸鸡原因,传感器状态,电机状态,控制信息,滤波信息做准备。本节主要分析xKF1日志信息,欢迎批评指正。学习好ardupilot必须会分析飞行日志,从飞行日志中我们可以学到很多无人机的知识。无人机是综合知识的体现,涵盖:导航、控制、滤波、算法、协议等各种知识,只有更全面的学习,才能在无人机的道路上越走越远。

陌城烟雨 1233

Valid Parenthesis

class Solution: def isValid(self, s: str) -> bool: dic = {'(':')','[':']','{':'}'} stack= [] for i in s: if i in dic.keys(): stack.append(i) elif stack: if dic[stack[-1.

具身机器人和通用AI 314

硬核实操!DeepSeek 携手 PCB 设计软件,自然语言绘就电路蓝图

摘要:本文深入探讨如何将 DeepSeek 与 PCB 设计软件对接,实现自然语言生成 PCB 电路图。文中详细阐述了从搭建对接架构,借助 Python 开发中间接口、适配数据格式,到自然语言处理,完成语义解析、知识匹配推理;再到生成 PCB 电路图,涵盖原理图与设计数据转化;最后对电路图进行优化与验证,包含布局布线优化和设计规则检查。尽管面临自然语言理解和复杂电路设计等挑战,但通过实操展示了技术可行性,为电子 / 嵌入式开发智能化设计提供新思路和完整实操指南。

专注于人工智能、软件开发、工控自动化、工厂数字化及智能化等领域,希望和大家共同进步! 4837

KEPServerEX作为MQTT客户端

打开软件: 测试没有连接上MQTT代理。

低码开发 1685

678. Valid Parenthesis String

Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules: Any left paren

SHURamos的博客 1014

37、基于铁电场效应晶体管(FeFET)的片上深度神经网络(DNN)直接反馈对齐训练

本文提出了一种基于铁电场效应晶体管(FeFET)的深度神经网络(DNN)训练加速器架构,结合直接反馈对齐(DFA)算法和FeFET的独特物理特性,以解决当前DNN训练中反向传播(BP)算法带来的高计算开销、复杂数据依赖和高能耗问题。通过设计基于FeFET的MVM引擎、低功耗ADC和随机数生成器(RNG),该架构在速度和功率效率方面相比现有基于ReRAM的加速器分别提升了1.3倍和2.5倍。研究为未来高效能AI训练硬件的发展提供了新的思路和实现路径。

wine的专栏 169

Leetcode 20 Valid Parenthesis

题目要求: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" a

傲龙残雪 751

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

【leetcode刷题】20. Valid Parentheses

Given a string containing just the characters'(',')','{','}','['and']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of b...

菜菜鸟的博客 195

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

leetcode 20. Valid Parentheses python3 实现

水题 其实就是模仿做个stack就好了 碰到符合条件的弹出 注意处理边界 因为边界的wa过 class Solution: def isValid(self, s: str) -> bool: if len(s) == 0: return True strList = list(s) stackList =...

qq_37021982的博客 302

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

【LeetCode】20. Valid Parentheses 有效的括号

【LeetCode】Valid Parentheses 解题报告标签(空格分隔): LeetCode题目地址:https://leetcode.com/problems/valid-parentheses/#/description题目描述:Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, d

负雪明烛 3113

(Java)leetcode-20 Valid Parentheses

题目 【括弧匹配】 Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same t...

不思量 248

[Leetcode][python]Valid Parentheses

题目大意判断括号开闭解题思路栈 简单题,思路正确即可代码class Solution(object): def isValid(self, s): """ :type s: str :rtype: bool """ stack = [] for char in s: if

蛮三刀酱 670

LeetCode - 678. Valid Parenthesis String (DP | 思维)

LeetCode - 678. Valid Parenthesis String (DP | 思维) DP 思维 题目链接 题目 解析 这个题目有很多种解法。是一道练思维的好题。 DP 记忆化的思路: 递归函数recur求[L, R]范围内的字符串是否可以构成解,答案是recur(0, n-1); 递归终止条件是L == R,如果此时chs[L] == '*',则按照题目要求是返回tru...

zxzxin的博客~ 439

LeetCode 20 Valid Parentheses (C,C++,Java,Python)

Problem: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}

算法之道 4334

LeetCode 678 Valid Parenthesis string

LeetCode 678 这个题目看起来不难,但是实际上我觉得还是挺容易错的。 看到parenthesis的validation题目,自然想到的是stack,如果没有 * ,每次看到(就push进去,看到)就pop,最后是空就对了。 但是有了*,可以任意替换,一种方法就是*单独存一个stack,遇到)的时候,先pop (的stack,没有的话pop *的stack。 方法上基本上正确,第一遍写的时...

frankguodongchen的博客 166

leetcode 678. Valid Parenthesis String【stack+logic,greedy,dynamic】

文章目录678. Valid Parenthesis String【stack+logic,greedy,dynamic】stack O(n)dynamic O(n^2)Greedy O(n)deque c++ 678. Valid Parenthesis String【stack+logic,greedy,dynamic】 Given a string s containing only three types of characters: ‘(’, ‘)’ and ‘*’, return true if

Wuuuuuu2019的博客 206

leetcode - 678. Valid Parenthesis String

leetcode - 678

sinat_41679123的博客 1041

LeetCode //C - 678. Valid Parenthesis String

【代码】LeetCode //C - 678. Valid Parenthesis String。

Made in Code 758

[LeetCode]Valid Parenthesis String

Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules: Any left paren

小熊听 311
上一篇: 算法-全排列
下一篇: Eclipse使用Tips-不定期更新
sheldonwong
博客等级 码龄14年 5粉丝 64原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值