LeetCode解题报告--Valid Parentheses

Xilinx Vivado LDPC IP core,TSN IP core. 各种IP core 阅读详情

**题目:**Valid Parentheses

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 valid but “(]”
and “([)]” are not.

分析:题意要求判断输入的符号(”()”,”{}”,”[]”)字符串是否是成对匹配。
改题用栈(stack)是最简单直接的解法,直接调用java内部stack类包。
1)新建一个栈,将输入的字符串中的“(”,“{”,“[”压入栈,
2)当遇到字符串中”)”或”}”或”]”,与栈最后一个元素进行判断,如如果当前的字符是”)”,则需要判断此时的栈顶元素是否为”)”,如果不是则直接返回false,结束程序;如果是则将栈顶元素pop,
3)循环执行2)中步骤,直至字符串遍历完成。

Java Accepted代码:

public class Solution {
    public boolean isValid(String s) {
        if (s.length() % 2 != 0) {
            return false;
        }

        Stack<Character> string = new Stack<Character>();
        char ch;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '[' || s.charAt(i) == '(' || s.charAt(i) == '{') {
                string.push(s.charAt(i));
                //System.out.println(string);
            } else {
                if (string.isEmpty()){
                    return false;
                }
                else {
                    ch = s.charAt(i);
                    switch (ch) {
                    case ']':
                        if (string.pop() != '[')
                            return false;
                        break;
                    case ')':
                        if (string.pop() != '(')
                            return false;
                        break;
                    case '}':
                        if (string.pop() != '{')
                            return false;
                        break;
                    }
                }

            }
        }
        if(string.isEmpty()){
            return true;
        }else{
            return false;
        }
    }
}

与此相关的内容在之前的博客《基于链表实现Java 自定义Stack栈》

相关代码放在个人github:https://github.com/gannyee/LeetCode/

C#使用OpenCvSharp+微信模型识别多个二维码 本文介绍了一个基于OpenCvSharp和微信开源二维码检测器的多二维码识别项目。该项目通过C# WinForm实现,主要功能包括:加载图片、识别图片中的多个二维码并显示识别结果。核心代码使用了OpenCvSharp.WeChatQRCode类,通过预训练的caffe模型(detect.prototxt/detect.caffemodel和sr.prototxt/sr.caffemodel)进行二维码检测和解码。识别结果包括每个二维码的编号、内容及位置信息。项目适用于批量二维码识别场景,部署简单,识别效果良 阅读详情

相关推荐

简单概述人形机器人的“大脑、小脑”

原创 仲翔云人形机器人产业通2024年09月13日 19:41上海人形机器人的大脑和小脑是其控制系统的关键组成部分,它们模拟了人类大脑和小脑的功能,以实现复杂的运动控制和决策过程。

AI生成曾小健3 2069

[leetcode]-20. Valid Parentheses(C语言)

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...

shen_zhu的博客 482

每天一个优秀提示词学习收藏 - 文本创作篇(二)

宝子们,今天不藏私啦!我把这段时间四处搜罗、潜心钻研出来的文本创作 Prompt 提示词大礼包双手奉上,这可都是能让你的 AI 创作水平原地起飞的干货!走过路过不要错过,赶紧码住,开启你的 AI 创作进阶之旅吧!

2397

【leetcode】第20题 Valid Parentheses 题目+解析+代码

【题目】 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 va

498

通达信量化打板选股指标公式源码副图

X_2:=3*LOW;X_13:=4;X_14:=3;X_15:=2;X_16:=1;

gpzb7的博客 4481

在Windows 11上用VirtualBox搞定WRF-Hydro 5.2.0:一个水文模型小白的Ubuntu 22.04虚拟机避坑实录

本文详细记录了在Windows 11上使用VirtualBox和Ubuntu 22.04虚拟机成功编译安装WRF-Hydro 5.2.0水文模型的全过程。从虚拟环境配置、依赖库安装到环境变量设置,作者分享了针对Linux新手的实用避坑技巧和关键命令,帮助科研人员高效完成水文模型的环境搭建。

weixin_30733003的博客 352

【LeetCode】Longest Valid Parentheses 解题报告

【题目】 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring i

跳出温水的青蛙 2378

ANSYS材料库实战:手把手教你用APDL命令流定义混凝土和形状记忆合金

本文详细介绍了在ANSYS中使用APDL命令流定义混凝土和形状记忆合金(SMA)的高级建模技巧。通过SOLID65单元和SMA本构模型,工程师可以精确模拟非线性材料行为,包括混凝土的开裂和SMA的超弹性效应。文章提供了完整的命令流示例和求解策略优化建议,帮助用户提升工程仿真效率。

hh234的博客 485

leetcode解题报告20. Valid Parentheses

leetcode解题报告20. Valid Parentheses题目地址 难度是easy题目描述给一条由“(){}[]”这6种字符组成的字符串,判断其是否合法,即一个左括号必须和其后面第一个右括号是同类型的。我的思路题目比较简单,难点在于想到用stack的后进先出的技巧来完成括号的配对。我的代码class Solution { public: bool isValid(string s)

sysu_xiaoming的博客 301

手把手教你用GD32F407VET6的EXMC驱动外部SRAM,搞定大内存需求

本文详细介绍了如何使用GD32F407VET6的EXMC外设驱动外部SRAM,解决嵌入式开发中的大内存需求问题。通过硬件连接方案、EXMC时序配置与初始化步骤,以及软件集成与内存管理技巧,帮助开发者快速实现内存扩展。特别适合需要处理图形显示缓冲、音频处理等大内存场景的GD32F407开发者。

weixin_30752699的博客 345

7.3 空间矢量调制(SVM)整数算法

本文提出了一种基于整数运算的空间矢量调制(SVM)优化实现方案,适用于计算资源有限的Cortex-M0平台。针对传统SVM算法中复杂的三角函数和浮点运算问题,重点优化了扇区判断和占空比计算两个关键环节。在扇区判断方面,采用基于电压分量符号和相对大小的三变量法,避免了角度计算;在占空比计算方面,通过几何关系将作用时间表示为电压分量的线性组合,并采用定点数实现。方案还详细讨论了系数定点化、死区补偿等实现细节,在保证精度的同时显著降低了计算复杂度。实验结果表明,优化后的整数算法在M0平台上仅需50-80个周期即可

技术工作者 942

LeetCode Longest Valid Parentheses 解题报告

分析,求出一串由:‘(’和‘)’组成的字符串中最长有效括号的长度。例如:(()(),结果是4。((()))结果是6。())()()结果是4。

算法的天空 1万+

【WRF安装】WRF环境配置移植详细说明

【WRF安装】WRF环境配置移植详细说明

WW、forever的博客 819

[LeetCode 解题报告]020.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...

CVer 173

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

数学建模模型总结

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

LeetCode解题报告—— Longest Valid Parentheses

Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring. Example 1: Input: "(()" Output: 2 Explanation: The longest...

dbvz4901的博客 131

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

LeetCode: Valid Parentheses 解题报告

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

weixin_34212189的博客 189

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

[leetcode] 20. Valid Parentheses 解题报告

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

小榕流光的专栏 639

[LeetCode] Valid Parentheses 解题报告

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 valid but

codingtmd_in_china的博客 218

LeetCode: Longest Valid Parentheses 解题报告

Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses ...

weixin_33824363的博客 158

[LeetCode] Longest Valid Parentheses 解题报告

原题链接 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()"

Coding Husky 417

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()",

小一的专栏 1728

[leetcode] 32. Longest Valid Parentheses 解题报告

题目链接:https://leetcode.com/problems/longest-valid-parentheses/ Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring

小榕流光的专栏 771

Leetcode Valid Parentheses 解题报告

这个就是判断括号的匹配,也很好写,限制也很少。。。 其实就是栈的使用,别的也没有什么,但是我的解法是把整个字符串都遍历一遍再去判空,其实不用,判断过程中只要有不匹配的就可以直接return class Solution { public: bool isValid(string s) { int len = s.size(); if(len%2!

weixin_37483932的博客 214

[LeetCode 解题报告]032. Longest Valid Parentheses

Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring. Example 1: Input: "(()" Output: 2 Explanation: The longest valid...

CVer 136

Leetcode 32 Longest Valid Parentheses 解题报告。

Leetcode 32 Longest Valid Parentheses 解题报告。题意 Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring. For “(()”

rjx博客 320

leetcode 32题 Longest Valid Parentheses 解题报告

leetcode algorithm 32题 Longest Valid Parentheses 解题报告 原题地址 这个题目一眼看过去就是用栈做,然后我就顺手写了个栈试了一些,结果就过了…… 然而这道题是归类在动态规划里面的,所以我后面会再尝试用动态规划的知识来解决,至少我花了两个小时还没有写出状态转移方程来,在看了答案之后才发现自己想的过于复杂 栈解法 用栈的话就很简单了,虽然很多括...

生息之地 262
上一篇: LeetCode解题报告--Remove Nth Node From End of List
下一篇: LeetCode解题报告--Merge Two Sorted Lists
YatKam
博客等级 码龄12年 58粉丝 49原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值