《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 ...
随机推荐
- 西邮Linux兴趣小组2014级免试挑战题
原文链接:西邮Linux兴趣小组2014级免试挑战题 又到了小组纳新的季节^_^,时间过的真快! 想起去年这会儿自己做免试题的时候根本找不着北@_@ 有幸今年能够在这里和大家分享免试挑战题,也正如我 ...
- Beyond Compare脚本:命令行批量比较文件并生成html格式的差异报告
BComp.exe /silent /closescript /solo @E:\compareTest\BCbatch.txt text-report layout:side-by-side opt ...
- delphi 动态加载dll
引入文件 DLL比较复杂时,可以为它的声明专门创建一个引入单元,这会使该DLL变得更加容易维护和查看.引入单元的格式如下: unit MyDllImport; {Import unit for MyD ...
- Replication--对发布修改的一些小总结
--==================================================== --在华丽分割线下,是我对肖磊--大菠萝的崇高地敬意和婶婶地感谢,本文乃肖兄表述我执笔而来 ...
- Ajax 访问 或 获取 IIS 虚拟目录
使用场景 最近用 .net core mvc 写了一个工具类的项目,作为我们项目的后台管理网站使用.第一次被老大拿去部署的时候被告知不可用,同样的代码在我电脑和我的iis上都可以使用的啊. 后来才知道 ...
- 使用ABP框架踩过的坑系列2
ABP中有很多惯例,如果使用得当,可以事半功倍,如果使用不当,也会有很大的麻烦,是否适当其实还是要看Need需求 ASP.NET Boilerplate (ABP) is an open source ...
- Prism 的 TabControl 导航
基于Prism 7.1 最近工作中可能会用到TabControl所以作为小菜的我提前预习了一下,结果并没有我想的那么简单,于是乎 各种网上查,本来用wpf的人就不多 prism 的可查的资料就更少的可 ...
- VisualStudio、NETFramework及C#版本关系
1.Visual Studio..NET Framework 及C#版本搭载关系介绍 Visual Studio版本 .NET Framework版本 C#版本 增加功能 Visual Studio ...
- JavaScript编程题(含腾讯2016校招题)
作者:ManfredHu 链接:http://www.manfredhu.com/2016/04/02/15-veryGoodForUsing/ 声明:版权所有,转载请保留本段信息,否则请不要转载 几 ...
- PageAdmin CMS网站建设教程:如何实现信息的定时发布
PageAdmin Cms发布文章时候有一个上线时间设置和下线时间设置,网站编辑人员可以利用这个功能来实现定时发布,在信息发布界面,如下图: 设置后就会自动加入定时任务中,注意这个功能需要再系统设置& ...