《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫《leetbook》的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/
20. Valid Parentheses
问题
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.
思路
这道题很简单,就是一个经典的栈的例子——表达式中的括号符号匹配。
- 遇见了左括号就进栈
- 遇见了右括号就出栈
- 如果栈为空,出错
- 如果出栈元素不是匹配的括号,出错
这里解决出括号匹配用了一个小tick,就是利用ASCII码,匹配的括号的ascii码都不会相差太远
- ‘(’ ‘)’ 相差1
- ‘[’ ‘]’ ‘{’ ‘}’ 相差2
public class Solution {
public boolean isValid(String s) {
if(s.length() == 0)
return false;
Stack<Character> stack = new Stack<Character>(); // 创建堆栈对象
for(int i = 0;i < s.length(); i++)
{
if(s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{')
stack.push(s.charAt(i));
if(s.charAt(i) == ')' || s.charAt(i) == ']' || s.charAt(i) == '}')
{
if(stack.empty()) return false;
char out = stack.pop();
if(out - s.charAt(i) > 2)
return false;
}
}
if(stack.empty())
return true;
return false;
}
}
《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题的更多相关文章
- leetcode题解:Valid Parentheses(栈的应用-括号匹配)
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- LeetCode题解(20)--Valid Parentheses
https://leetcode.com/problems/valid-parentheses/ 原题: Given a string containing just the characters ' ...
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- C# 写 LeetCode easy #20 Valid Parentheses
20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- LeetCode 之 Longest Valid Parentheses(栈)
[问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...
- 【LeetCode】20. Valid Parentheses 有效的括号
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...
- 【一天一道LeetCode】#20. Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 20. Valid Parentheses[E]有效的括号
题目 Given a string containing just the characters '(',')','[',']','{' and '}',determine if the input ...
随机推荐
- hdu 4983 欧拉函数
http://acm.hdu.edu.cn/showproblem.php?pid=4983 求有多少对元组满足题目中的公式. 对于K=1的情况,等价于gcd(A, N) * gcd(B, N) = ...
- colorbox在android上由于高度太高无法scroll问题
首先看问题截图: 我们可以看到弹出的colorbox页面的高度已经到了下面,你根本看不到"mistake cross"<=>"X". 我测试了iph ...
- Delphi XE5 图解为Android应用制作签名
http://redboy136.blog.163.com/blog/static/107188432201381872820132 Delphi XE5 图解为Android应用制作签名 2013- ...
- NotMapped属性特性
NotMapped特性可以应用到领域类的属性中,Code-First默认的约定,是为所有带有get,和set属性选择器的属性创建数据列.. NotManpped特性打破了这个约定,你可以使用NotMa ...
- asp.net Mvc 模型绑定项目过多会导致页面运行时间卡
asp.net Mvc 模型绑定项目过多会导致页面运行时间卡的问题. 解决方式就是采用ModelView方式进行精简,已减少模型绑定及验证的时间.
- Alwayson--配置只读请求
场景:有三台服务器SQLNode131,SQLNode132 ,SQLNode133: 1) 三台服务器加入域DCDemo.com 2) 三台服务器使用端口访问数据库 3) 三台服务器构建故障转移群集 ...
- ABP 框架代码批量生成器
需要最新源码,或技术提问,请加QQ群:538327407 我的各种github 开源项目和代码:https://github.com/linbin524 简介 用abp 框架快两年了,用它完成了多个项 ...
- 使用客户端软件向服务端php程序发送post数据,php接受三种方法
方法一:$_POST; 方法二:$GLOBALS['HTTP_RAW_POST_DATA'],需要在php.ini开启 always_populate_raw_post_data = On: 方法三: ...
- 【转】Java并发编程:线程池的使用
Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...
- 三个分段的.tar.gz文件,合并并解压
1.合并使用spilt分割的文件 # cat sxrt5.0.dvd1.tar.gzaa sxrt5.0.dvd1.tar.gzab sxrt5.0.dvd1.tar.gzac >>sxr ...