首页 > 其他分享 >Valid Parentheses

Valid Parentheses

时间:2023-06-13 13:45:11浏览次数:28  
标签:Parentheses string brackets mapping char Valid Input stack

Given a string s 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 must be closed in the correct order.
Every close bracket has a corresponding open bracket of the same type.

Example 1:

Input: s = "()"
Output: true

Example 2:

Input: s = "()[]{}"
Output: true

Example 3:

Input: s = "(]"
Output: false

Solution:

class Solution(object):
    def isValid(self, s):  
        stack = []  
        mapping = {')': '(', '}': '{', ']': '['}  
        for char in s:  
            if char in mapping:  # 判断char是否为字典mapping的键
                # 这种处理方式可以避免在栈为空时尝试弹出元素而引发的异常
                top = stack.pop() if stack else '#'  
                if mapping[char] != top:  
                    return False  
            else:  
                stack.append(char)  
        return not stack

标签:Parentheses,string,brackets,mapping,char,Valid,Input,stack
From: https://www.cnblogs.com/artwalker/p/17477284.html

相关文章

  • 同一实体多字段的不同校验--validate
    validate的使用,同一实体分组校验ValidatorFactory文章目录前言一、举个例子二、使用步骤1.引入依赖2.加入分组标识3.分组校验应用1.明确知道当前接口方法(新增/修改)2.不明确当前接口方法(修改增加为同一个接口)总结前言实际开发中遇到这样一个问题,一个接收参数的实体类,......
  • @Validated注解和@Valid注解区别
    引入依赖注意:spirngboot升级到2.3.0.RELEASE之后,hibernate-validator不再作为spring-boot-starter-web的默认依赖项,需要通过下面的maven坐标单独引入:<dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><versio......
  • vue报错Invalid VNode type: undefined
    报错项目启动后,部分内容没有显示出来。打开console后,显示[Vuewarn]:InvalidVNodetype:undefined(undefined)处理引入“defineAsyncComponent”实现异步引入。import{defineAsyncComponent}from'vue'。问题解决了。......
  • Handling Invalid Characters in an XML String (zz.IS2120.BG57IV3)
    Thereare5predefinedentityreferencesinXML://z2013-08-2018:03:[email protected][T191,L2147,R75,V2925]<<lessthan>>greaterthan&amp;&ampersand &apos;'apostrophe""quotationmark//z2014-04-1017:47......
  • validation校验注解
    空检查@Null验证对象是否为null@NotNull验证对象是否不为null,无法查检长度为0的字符串@NotBlank检查约束字符串是不是Null还有被trim的长度是否大于0,只对字符串,且会去掉前后空格.@NotEmpty检查约束元素是否为NULL或者是EMPTY.布尔检查说明......
  • 【HMS Core】华为帐号服务,获取Access Token报错{sub_error:20152,error_description:inv
    ​ 【问题描述】华为账号服务,接口获取AccessToken报错:{sub_error:20152,error_description:invalidcode,error:1101} 【问题分析】根据官网提示,是code格式不正确造成的,需要检查参数配置​ 【解决方案】1、此问题解决方案,可以参考这篇帖子https://developer.huawei.com/......
  • 【HMS Core】华为帐号服务,获取Access Token报错{sub_error:20152,error_description:inv
     【问题描述】华为账号服务,接口获取AccessToken报错:{sub_error:20152,error_description:invalidcode,error:1101}【问题分析】根据官网提示,是code格式不正确造成的,需要检查参数配置【解决方案】1、此问题解决方案,可以参考这篇帖子https://developer.huawei.com/consumer/cn/forum/......
  • 开发密码登陆接口用postman测试报错“key is of invalid type”
    发现为go中jwt使用错误我出错的地方为//出现错误地方为tokenClaims:=jwt.NewWithClaims(jwt.SigningMethodES256,claims)returntokenClaims.SignedString(jwtSecret)我出错的点:加密方式选择了 jwt.SigningMethodES256,应该选择jwt.SigningMethodHS256,这个H是hash的意......
  • golang导入私有仓库报错:“server response: not found:xxx: invalid version: git ls
    1.问题:goget导入私有仓库报错➜goget"devops.gitlab.xxx.com/test/kafka-utils"go:devops.gitlab.xxx.com/test/[email protected]:verifyinggo.mod:devops.gitlab.xxx.com/testo/[email protected]/go.mod:readinghttps://goproxy.cn/sumdb/sum.golang.org/......
  • spring-boot-starter-validation数据校验
    依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId></dependency> beanimportboot.annotation.Status;importlombok.AllArgsConstructor;import......