Valid Parentheses leetcode java
题目:
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.
题解:
这道题是一道很常见的老题,记得当时看严蔚敏的数据结构还有数据结构课上面都见过这道题,一道训练栈的基础题。
解题方法是:
一个个检查给的characters,如果是左括号都入栈;如果是右括号,检查栈如果为空,证明不能匹配,如果栈不空,弹出top,与当前扫描的括号检查是否匹配。
全部字符都检查完了以后,判断栈是否为空,空则正确都匹配,不空则证明有没匹配的。
注意:
检查字符是用==,检查String是用.isEqual(),因为String是引用类型,值相等但是地址可能不等。
代码如下:
1 public boolean isValid(String s) {
2 if(s.length()==0||s.length()==1)
3 return false;
4
5 Stack<Character> x = new Stack<Character>();
6 for(int i=0;i<s.length();i++){
7 if(s.charAt(i)=='('||s.charAt(i)=='{'||s.charAt(i)=='['){
8 x.push(s.charAt(i));
9 }else{
if(x.size()==0)
return false;
char top = x.pop();
if(s.charAt(i)==')')
if(top!='(')
return false;
else if(s.charAt(i)=='}')
if(top!='{')
return false;
else if(s.charAt(i)==']')
if(top!='[')
return false;
}
}
return x.size()==0;
}
Valid Parentheses leetcode java的更多相关文章
- Longest Valid Parentheses leetcode java
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- Valid Parentheses - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Valid Parentheses - LeetCode 注意点 考虑输入为空的情况 解法 解法一:如果是'('.'{'.'['这三者就入栈,否则就判断栈 ...
- Valid Parentheses [LeetCode 20]
1- 问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...
- Longest Valid Parentheses - LeetCode
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Leetcode 20题 有效的括号(Valid Parentheses) Java语言求解
题目描述: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空 ...
- Generate Parentheses leetcode java
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- Valid Sudoku leetcode java
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- Valid Palindrome leetcode java
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- Valid Number leetcode java
题目: Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...
随机推荐
- spring boot mybatis 多数据源配置
package com.xynet.statistics.config.dataresources; import org.springframework.jdbc.datasource.lookup ...
- MapReduce与批处理------《Designing Data-Intensive Applications》读书笔记14
之前的文章大量的内容在和大家探讨分布式存储,接下来的章节进入了分布式计算领域.坦白说,个人之前专业的重心侧重于存储,对许多计算的内容理解可能不是和确切,如果文章中的理解有所不妥,愿虚心赐教.本篇将和大 ...
- 这套完美的Java环境安装教程,完整,详细,清晰可观,让你一目了然,简单易懂。⊙﹏⊙
JDK下载与安装教程 2017年06月18日 22:53:16 Danishlyy1995 阅读数:349980 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...
- [Luogu5162]WD与积木(多项式求逆)
不要以为用上Stirling数就一定离正解更近,FFT都是从DP式本身出发的. 设f[i]为i个积木的所有方案的层数总和,g[i]为i个积木的方案数,则答案为$\frac{f[i]}{g[i]}$ 转 ...
- BZOJ 2333: [SCOI2011]棘手的操作 可并堆 左偏树 set
https://www.lydsy.com/JudgeOnline/problem.php?id=2333 需要两个结构分别维护每个连通块的最大值和所有连通块最大值中的最大值,可以用两个可并堆实现,也 ...
- BZOJ.3110.[ZJOI2013]K大数查询(整体二分 树状数组/线段树)
题目链接 BZOJ 洛谷 整体二分求的是第K小(利用树状数组).求第K大可以转为求第\(n-K+1\)小,但是这样好像得求一个\(n\). 注意到所有数的绝对值\(\leq N\),将所有数的大小关系 ...
- Tomcat CVE-2017-12615 远程上传漏洞复现
漏洞名称:CVE-2017-12615-远程代码执行漏洞 CVE-2017-12615:远程代码执行漏洞 当 Tomcat运行在Windows操作系统时,且启用了HTTP PUT请求方法(例如,将 r ...
- java多线程技术之八(锁机制)
Lock是java.util.concurrent.locks包下的接口,Lock 实现提供了比使用synchronized 方法和语句可获得的更广泛的锁定操作,它能以更优雅的方式处理线程同步问题,我 ...
- bzoj 1176: [Balkan2007]Mokia&&2683: 简单题 -- cdq分治
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MB Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要 ...
- TeamViewer运行在Windows Server 2008下连接时错误提示:正在初始化显示参数
这个是使用远程桌面安装和使用Teamviewer的问题,解决方法: 实际上安装完成后TeamViewer有两个ID,一个是个人ID(就是上面卡住的780 567 914),另一个是服务器ID,我们通过 ...