leetcode 20 Valid Parentheses 有效的括号
描述:
给定一些列括号,判断其有效性,即左括号有对应的有括号,括号种类只为小,中,大括号。
解决:
用栈。
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 有效的括号的更多相关文章
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...
- [LeetCode]20. Valid Parentheses有效的括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 【LeetCode】20. Valid Parentheses 有效的括号
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...
- leetcode 20 Valid Parentheses 括号匹配
Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...
- LeetCode 20 Valid Parentheses (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
随机推荐
- timer Compliant Controller project (4)layout and gerber, paning
1 LAYOUT 2 Gerber 3 CAM350-Paining
- PyQt4 HardwareManager
# PyQt4 HardwareManager # 声明: # 本软件主要是由于朋友说想要一个产品缺陷记录软件,主要用于记录产品缺陷, # 通过产品序列号进行插入.查询,本来想用VC++ 6.0做,但 ...
- JQ深度手记、源码分析
1.$.extend() 对象继承操作.浅拷贝操作.深拷贝操作(第一个参数:true) var a = { name:'lisan' }; var b = {}; $.extend(b, a); // ...
- It is the courage
It is the reality that a society which becomes lower and becomes weak.Believe it or not,I think it i ...
- 【算法】通过TreeMap理解红黑树
本文以Java TreeMap为例,从源代码层面,结合详细的图解,剥茧抽丝地讲解红黑树(Red-Black tree)的插入,删除以及由此产生的调整过程. 总体介绍 Java TreeMap实现了So ...
- ProcessHelp 进程类(启动,杀掉,查找)
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...
- tomcat 注冊成操作系統服務
nginx注冊成服務1.把srvany.exe和instsrv.exe拷貝到nginx安裝路徑下面.2.執行命令Command代碼instsrv Nginx D:\nginx\srvany.exe3. ...
- cowboy添加验证码
参考的http://beebole.com/blog/erlang/how-to-implement-captcha-in-erlang-web-application/,移到cowboy,修改了下: ...
- emacs之配置2,UI基本设置
在-下建立目录emacsConfig,里面建立一些自己写的el脚本,下面是名字随便,我的 emacsConfig/ui-setting.el ;关闭Emacs的默认启动界面 (setq inhibit ...
- [MVC 4] ActionResult 使用示例
在控制器 HomeController.cs 中使用以下代码 public ActionResult Contact() { ViewBag.Message = "Your contact ...