题目描述

给出一个仅包含字符'(',')','{','}','['和']',的字符串,判断给出的字符串是否是合法的括号序列
括号必须以正确的顺序关闭,"()"和"()[]{}"都是合法的括号序列,但"(]"和"([)]"不合法。

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.

示例1

输入

复制

"["

输出

复制

false
示例2

输入

复制

"[]"

输出

复制

true

class Solution {
public:
    /**
     *
     * @param s string字符串
     * @return bool布尔型
     */
    bool isValid(string s) {
        // write code here
        int len=s.size();
        stack <char> sta;
        for (int i=0;i<len;i++){
            if (s[i]=='(')sta.push(')');
            else if (s[i]=='[') sta.push(']');
            else if (s[i]=='{')sta.push('}');
            else if (sta.empty())return false;
            else if (sta.top()!=s[i])return false;
            else sta.pop();
            
        }
        return sta.empty();
    }
};

leetcode129valid-parentheses的更多相关文章

  1. [LeetCode] Remove Invalid Parentheses 移除非法括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  2. [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  3. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  4. [LeetCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  5. [LeetCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  6. LeetCode 22. Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  7. 【leetcode】Generate Parentheses

    题目简述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  8. No.022:Generate Parentheses

    问题: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...

  9. 【LeetCode】241. Different Ways to Add Parentheses

    Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...

  10. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

随机推荐

  1. Matlab中image、imagesc和imshow函数用法解析

    来源:https://blog.csdn.net/zhuiyuanzhongjia/article/details/79621813 1.显示RGB图像 相同点:这三个函数都是把m*n*3的矩阵中的数 ...

  2. JVM 第二篇:垃圾收集器以及算法

    本文内容过于硬核,建议有 Java 相关经验人士阅读. 0. 引言 一说到 JVM ,大多数人第一个想到的可能就是 GC ,今天我们就来聊一聊和 GC 关系最大的垃圾收集器以及垃圾收集算法,希望能通过 ...

  3. 【linux】基础命令一

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mount dir[] device[]umount devic[]maste ...

  4. k8s-获取kuboardtoken

    master节点执行命令 echo $(kubectl -n kube-system get secret $(kubectl -n kube-system get secret | grep kub ...

  5. centos8环境安装配置rsync

    一,查看本地centos的版本: [root@localhost lib]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core) ...

  6. dom4j api 详解【转】

    1.DOM4J简介 DOM4J是 dom4j.org 出品的一个开源 XML 解析包.DOM4J应用于 Java 平台,采用了 Java 集合框架并完全支持 DOM,SAX 和 JAXP. DOM4J ...

  7. CentOS8安装本地mail工具-mailx-12.5-29.el8.x86_64

    概述 服务器需要发告警邮件 查找是否已安装 [root@C8-1 ~]# type mail -bash: type: mail: not found [root@C8-1 ~]# which mai ...

  8. python第三章:函数

    在前面章节中,介绍了一些input(),print(),len()等内建函数,还有random,math等标准库相关函数,这些都是可以直接使用的,但是很多时候,我们也是可以编写自己的函数. 看个例子: ...

  9. ASP.NET CORE 开发微信公众号(一、测试号管理)

    一.注册账号 百度微信公众平台,点击进入. 二.公众平台测试账号 点击进入平台后居然是小程序,我也很费解.以前是找到开发->开发者工具->公众平台测试账号,现在毛都没有了. 不过可以点击这 ...

  10. MapReduce工作原理详解

    文章概览: 1.MapReduce简介 2.MapReduce有哪些角色?各自的作用是什么? 3.MapReduce程序执行流程 4.MapReduce工作原理 5.MapReduce中Shuffle ...