20. Valid Parentheses [easy] (Python)

车载软件刷写工具vFlash --- 自动化接口(Automation API)应用简介 阅读详情

题目链接

https://leetcode.com/problems/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.

题目翻译

给定一个字符串,只包含 '(', ')', '{', '}', '[' 和 ']' 这几个字符,判断输入字符串是否是有效的。
所有的括号必须以正确的顺序闭合,比如 "()" 和 "()[]{}" 是有效的,但 "(]" 和 "([)]" 是无效的。

思路方法

思路一

用栈来操作,将所有的字符依次入栈,当栈顶的括号和正要入栈的括号匹配时将栈顶的括号弹出且不入栈,否则入栈新的括号。最后,只有当栈里没有括号时,才表明输入是有效的。

代码

class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        pars = [None]
        parmap = {')': '(', '}': '{', ']': '['}
        for c in s:
            if c in parmap and parmap[c] == pars[len(pars)-1]:
                pars.pop()
            else:
                pars.append(c)
        return len(pars) == 1

思路二

实际上,通过观察可以发现:如果正要入栈的是右括号,而栈顶元素不是能与之消去的相应左括号,那么该输入字符串一定是无效的。于是,可以大的加快判断过程。

代码

class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        pars = [None]
        parmap = {')': '(', '}': '{', ']': '['}
        for c in s:
            if c in parmap:
                if parmap[c] != pars.pop():
                    return False
            else:
                pars.append(c)
        return len(pars) == 1

说明
当然这里使用dict来存放括号的匹配关系不是必要的,直接通过字符之间ascii码的差值判断也是比较快的。

PS: 新手刷LeetCode,新手写博客,写错了或者写的不清楚还请帮忙指出,谢谢!
转载请注明:http://blog.csdn.net/coder_orz/article/details/51697963

3、固定翼无人机模型解析 本文详细解析了固定翼无人机的动力学模型与纵向模型,涵盖位置、速度、角度及角速度的变化率方程,并介绍了力和力矩的计算方法以及气动系数的表达式。通过关键参数分析,探讨了速度、迎角和倾斜角对飞行性能的影响。文章还展示了模型在编队飞行和任务规划中的应用,并提出了参数优化与先进控制策略等优化思路,为无人机飞行控制系统设计提供了理论基础。 阅读详情

相关推荐

Windows上使用Docker Desktop部署ClickHouse的完整指南

数据库的容器化部署已成为本地开发和测试的主流实践,尤其对于列式数据库这类对运行环境有严格要求的系统而言,容器技术能有效屏蔽底层差异。Docker Desktop 基于 WSL2 后端,在 Windows 上提供了接近原生的 Linux 运行环境,使得 ClickHouse 这类官方不支持 Windows 原生安装的分析型数据库,也能以标准方式快速启动。容器化带来的不仅是环境的轻量化隔离,更关键的是数据持久化与版本管理能力——通过挂载宿主机目录,即使容器被删除,业务数据依然安全留存,从而让本地环境与生产服务器

weixin_30983353的博客 245

Valid Parentheses(有效的括号)

LeetCode 2. Valid Parentheses(有效的括号) 给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串,判断字符串是否有效。 有效字符串需满足: 左括号必须用相同类型的右括号闭合。 左括号必须以正确的顺序闭合。 注意空字符串可被认为是有效字符串。 示例 1: 输入: “()” 输出: true 示例 2: 输入: “()[]{}” 输出: true 示例...

coldcoffee001的博客 475

生信物种注释软件Kraken2+Bracken的安装与使用

一、软件安装与数据库下载 1.Kraken2安装 Kraken2是一个基于k-mer算法的高精度宏基因组序列分类软件,能够快速的将测序reads进行物种分类。 使用conda一键安装,要先安装好anaconda或者miniconda conda install -y kraken2 输入krakek2 --help 出现如下所示帮助文档,则表示安装成功。 2.Bracken安装 bracken是一种从宏基因组数据中高度准确的计算物种丰度的统计方法。 同样使用conda一键安装 conda install -

jileiqwd的博客 1万+

Valid Parentheses基本方法和几种优质解法解析

今天所做的习题是LeetCode20,Valid Parentheses,有很大的收获。 第一个想到的就是利用栈 先从左到右遍历字符串,找到所有的左部放到栈中, 再从右到左遍历字符串,找到所有的右部放到栈中, 之后开始进行匹配 发现不匹配立刻返回FALSE 到一方空了之后 如果另一方也空了就返回TRUE 否则返回FALSE 后来发现这种思路是不对的,因为: ([[]()

yinyanting1的博客 2041

DNN加速器脉动阵列轻量级瞬时故障检测

本文系统综述了面向深度神经网络(DNN)脉动阵列加速器的轻量级瞬时故障检测技术。通过分析比较算法级校验(ABFT)、数值异常监测(剪裁、中值滤波等)、硬件编码检错(AN码、剩余码)以及选择性冗余等方案,指出这些方法能以5%-10%的开销实现对权重和激活比特翻转的高效检测,远低于传统ECC/冗余计算的成本。研究显示,结合多种轻量技术可构建多层防护体系,在保障推理可靠性的同时保持加速器的高性能特性。该成果为安全关键领域的DNN加速器设计提供了重要参考。

欢迎交流! 1079

iTunes登录协议抓包避坑指南:解决HTTPDebugger被检测问题(附临时方案)

本文详细解析了iTunes客户端登录协议抓包时遇到的HTTPDebugger等工具被检测的问题,并提供了实战解决方案。通过逆向分析iTunes的反调试与反检测机制,文章指导读者如何定位并绕过关键检测点,成功捕获登录流量,并深度解析了包括证书获取、签名缓冲区和最终认证在内的核心协议流程。

qsc901234的博客 812

算法:匹配有效的括号20. Valid Parentheses

说明 算法:Valid Parentheses LeetCode地址:https://leetcode.com/problems/valid-parentheses/ 题目: Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is ...

AI架构师易筋 711

全志V3S嵌入式驱动开发(uboot移植)

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】 安装了ubuntu操作系统,有了开发板,下面就可以进行我们的开发工作了。第一步,我们要面临的问题就是uboot移植。一般来说,嵌入式的基础环境就是uboot、linux和rootfs。uboot完成一些基本硬件的初始化,比如cpu频率设置、ddr初始化、加载linux等等。早前,uboot的烧入比较麻烦,需要jtag工具才能完成。现在来说,很多soc支持系统直接从sd卡启动,这对开发

嵌入式-老费,一个分享专业嵌入式知识的blog 2826

[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, "()" an

李瑞远-时空数据 771

人工智能创业数据管理的重要角色

人工智能,数据管理,创业,数据治理,数据安全,数据分析,数据可视化 1. 背景介绍 在当今数据爆炸的时代,数据已成为企业最重要的资产之一。人工智能(AI)技术的快速发展,为数据管理带来了新的机遇和挑战。AI技术能够帮助企业从海量数据中挖掘价值,提高决策效率,提升运营水平。然而,数据管

AGI×大数据,开启智能时代的认知跃迁;解码AGI,赋能数据驱动的智能革命。 930

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

LeetCodet题解--20. Valid Parentheses(括号匹配问题)

链接 LeetCode题目:https://leetcode.com/problems/valid-parentheses/ GitHub代码:https://github.com/gatieme/LeetCode/tree/master/019-RemoveNthNodeFromEndOfList CSDN题解:http://blog.csdn.net/gatieme/ar

OSKernelLAB(gatieme) 1864

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

leetcode 20. Valid Parentheses——python(easy)——使用了堆栈

题目来源:                                  https://leetcode.com/problems/valid-parentheses/description/ 题目分析:    本题的含义是给定一个关于括号的字符串,然后判断括号的匹配是否成功。我觉得本题首先理解题意非常重要。首先可以看几个例子:       (((()))           T

revivre的博客 609

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Leet Code OJ 20. Valid Parentheses [Difficulty: Easy] -python

20.Valid Parentheses [Easy] 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 brackets. Open brackets m...

渣渣琪的博客 252

[leetcode]20. Valid Parentheses ,有效的括号符串python实现【easy】

题目: 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

zl87758539的专栏 995

Leet Code OJ 20. Valid Parentheses [Difficulty: Easy] -- python3

很简单的一道题,考点就是栈的使用,但是题目中有几个陷阱: 考虑空输入 非空输入保证成对,正常顺序出现 非空输入保证出来这几种括号之外没有其他符号的出现 可以加入几个提前判断 是否非空(第一次没有考虑到非空问题所以就wa了) 是不是只含有括号(使用正则表达式) 初步判断是不是成对出现,对长度进行判断。 在做了基本处理之后,本题的重点就变成判断是不是按照顺序出现。如果是‘{(【...

啊_三雨啊的博客 253

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

StudingCoder的博客 317

leetcode 1-100 easy难度题目汇总

主要是考虑到easy题目没什么好分析的, 故统一总结在一篇文章里. 留作记录. 收录leetcode1文章目录1. Two Sum (Easy)7. Reverse Integer (Easy)9. Palindrome Number (Easy)13. Roman to Integer (Easy)14. Longest Common Prefix (Easy)20. Valid Parenth...

Bean771606540的博客 1519

Leetcode#20 Valid Parentheses -Easy

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

swedishsunset的专栏 171

[LeetCode] 020. Valid Parentheses (Easy) (C++/Java/Python)

[LeetCode] 020. Valid Parentheses (Easy) (C++/Java/Python)

水果君の日常 3869

Leetcode - 20 - Valid Parentheses (easy)

Given a stringscontaining 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 brackets. Open brackets must be closed in the correct ...

xin_rong的博客 270

[LeetCode]easy - Valid Parentheses - python

Problem Description: Given a stringscontaining 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 brackets. Open brackets must b...

weixin_41873294的博客 150

LeetCode算法题之20. Valid Parentheses(easy)

声明: 如标题所示,因本人为AI路上的新手,文章用于辅助个人的整理记忆,理解难免有偏差之处,都是个人拙见,如给其他同僚造成困扰,还请见谅,非常非常非常欢迎私信共同讨论,共同进步 题目描述: Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input str...

DaMoWangZQ的博客 248

[Stack]20. Valid Parentheses, Easy

20

QQ_VV的博客 152

LeetCode小白菜笔记[6]:Valid Parentheses

LeetCode小白菜笔记[6]:Valid Parentheses 20. Valid Parentheses [Easy] 题目:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The bracke

VisualCortex 577

Valid Parentheses/easy/day5

题目: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 br...

u010969088的博客 155
上一篇: 290. Word Pattern [easy] (Python)
下一篇: 58. Length of Last Word [easy] (Python)
coder_orz
博客等级 码龄14年 102粉丝 90原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值