描述:

给定一些列括号,判断其有效性,即左括号有对应的有括号,括号种类只为小,中,大括号。

解决:

用栈。

bool isValid(string s) {
stack<char> st;
for (auto i : s) {
if (i == '(' || i == '[' || i == '{') {
st.push(i);
continue;
} else {
if (st.empty())
return false;
char c = st.top();
if (i == ')' && c != '(' ||
i == ']' && c != '[' ||
i == '}' && c != '{')
return false;
st.pop();
}
}
if (st.empty())
return true;
return false;
}

leetcode 20 Valid Parentheses 有效的括号的更多相关文章

  1. leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法

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

  2. [LeetCode]20. Valid Parentheses有效的括号

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

  3. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  4. [LeetCode] 20. Valid Parentheses 验证括号

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

  5. [LeetCode] 20. Valid Parentheses 合法括号

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

  6. [leetcode]20. Valid Parentheses有效括号序列

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

  7. 【LeetCode】20. Valid Parentheses 有效的括号

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...

  8. leetcode 20 Valid Parentheses 括号匹配

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

  9. LeetCode 20 Valid Parentheses (括号匹配问题)

    题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description   Problem: 括号匹配问题. 使用栈,先进后出!   ...

随机推荐

  1. window.onload 和 body.onload 相互覆盖的本质

    从根源上讲,window.onload和<body onload="alert('test');"> 所绑定的对象都是window ,body是没有onload事件的, ...

  2. 错误 1 类,结构或接口成员声明中的标记"="无效

    using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace Console ...

  3. 找到链表的倒数第k个节点 python

    题目:给定一个链表的头节点,输出链表倒数第k个节点的值 分析:最简单的思路就按顺序访问链表节点,得到链表的长度x之后,再次从头节点出发,访问到第x-k+1个节点时,就是链表倒数第k个节点,但是这样的方 ...

  4. C# 调用C++ DLL 的类型转换(转载版)

    最近在做视频监控相关的demo开发,实现语言是C#,但视频监控的SDK是C++开发的,所以涉及到C#调用C++的dll库.很多结构体.参数在使用时都要先进行转换,由非托管类型转换成托管类型后才能使用. ...

  5. NET Core的代码安全分析工具 - Security Code Scan

    NET Core的代码安全分析工具 - Security Code Scan https://www.cnblogs.com/edisonchou/p/edc_security_code_scan_s ...

  6. Oracle简单的SQL处理

    --单表插入insert into hr.job(job_id,job_title,min_salary) values('IT','Project Manager',50000); insert i ...

  7. PAT 1021 个位数统计 C语言

    1021. 个位数统计 (15) 给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0) ...

  8. vue-cli 本地开发mock数据使用方法

    vue-cli 中可以通过配置 proxyTable 解决开发环境的跨域问题,具体可以参考这篇文章: Vue-cli proxyTable 解决开发环境的跨域问题 如果后端接口尚未开发完成,前端开发一 ...

  9. StreamSets Data Collector Edge 说明

    Data Collector Edge 是不包含界面的agent 安装 下载包 https://streamsets.com/opensource tar xf streamsets-datacoll ...

  10. Git自动换行符

    http://blog.csdn.net/jonathan321/article/details/51988242?locationNum=2 不同的操作系统有不同的换行符格式,跨平台协作时需要考虑版 ...