class Solution {
public:
bool isValid(string s) {
stack<char> brackts;
for(int i = ; i < s.size(); i++)
{
if(s[i] == '(' || s[i] == '[' || s[i] == '{')
brackts.push(s[i]);
else if(brackts.empty() || s[i] == ')' && brackts.top() != '(' || s[i] == ']' && brackts.top() != '[' || s[i] == '}' && brackts.top() != '{')
return false;
else brackts.pop();
}
return brackts.empty();
}
};

用栈来解决,比较简单,匹配对称即可。

leetcode个人题解——#20 Valid Parentheses的更多相关文章

  1. 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  2. LeetCode解题笔记 - 20. Valid Parentheses

    这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...

  3. leetCode练题——20. Valid Parentheses

    1.题目 20. Valid Parentheses——Easy  Given a string containing just the characters '(', ')', '{', '}',  ...

  4. LeetCode题解(20)--Valid Parentheses

    https://leetcode.com/problems/valid-parentheses/ 原题: Given a string containing just the characters ' ...

  5. LeetCode记录之20——Valid Parentheses

    09.18更新算法采用栈的思想解决,方法①所示. 本题主要是找是否有匹配的字符串,因为还没有复习到栈之类的知识点,只能还是采用暴力方法了,后期会补上更加优化的算法.我的思路就是先遍历一遍找是否有匹配的 ...

  6. &lt;LeetCode OJ&gt; 20. Valid Parentheses

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

  7. 【leetcode❤python】 20. Valid Parentheses

    #-*- coding: UTF-8 -*-#利用栈的思想#如果输入的左测扩则入栈,如果输入为右侧扩,判断其是否与当前栈顶配对,如果匹配则删除这一对,否则return False#'(', ')', ...

  8. [Leetcode][Python]20: Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...

  9. 20. Valid Parentheses【leetcode】

    20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

随机推荐

  1. meta的随堂笔记

    Meta标签与搜索引擎优化(SEO) 概要 通常所说的meta标签,是在Html网页源代码中的一个重要的html标签.meta标签用来描述一个html网页文档的属性,例如作者.日期和时间.网页描述.关 ...

  2. Oracle数据库,简单SQL练习与答案

    1.数据 --创建职员表create table tbEmp( eID number primary key, --职员编号 eName varchar2(20) not null, --职员姓名 e ...

  3. Elasticsearch 5.x安装

    node1 elasticsearch node2 elasticsearch node3 elasticsearch 前期准备 JDK1.8 修改/etc/security/limits.conf ...

  4. git 项目常用

    本地分支推送到远程分支: (1),git init,git add . (2),git commit -m "首次提交" (3),git remote add origin '远程 ...

  5. python打印九九乘法表

    每种编程语言都可能会遇到编写“九九乘法表”的问题,用Python来处理也是很简单的,具体代码如下(基于Python3)): i = 1 while i <= 9: j = 1 while j & ...

  6. hive 入门

    hive-site.xml 配置 <configuration> <property> <name>javax.jdo.option.ConnectionURL&l ...

  7. HDFS原理

    1 . NameNode 概述 a. NameNode 是 HDFS 的核心. b. NameNode 也称为 Master. c. NameNode 仅存储 HDFS 的元数据:文件系统中所有文件的 ...

  8. 【Keil】Keil5的安装和破...

    档案的话网上很多的,另外要看你开发的是哪种内核的芯片 如果是STC的,就安装C51 如果是STM的,就安装MDK 当然市面上有很多芯片的,我也没用过那么多种,这里也就不列举了 至于注册机,就是...恩 ...

  9. 树莓派编译程序时报错:virtual memory exhausted: Cannot allocate memory

    一.原因分析: 树莓派内存太小,编译程序会出现virtual memory exhausted: Cannot allocate memory的问题,可以用swap扩展内存的方法. 二.解决方法: 安 ...

  10. 使用IPython调试代码

    从知乎作者Rui L学来的一招. 应该用过 IPython 吧?想象一下,抛出异常时自动把你带到 IPython Shell 是不是很开心?而且和普通的IPython不同,这个时候可以调用 p (pr ...