leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
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.
思路:题目整体上比較明白,是对括号是否有效做推断,算法上是用栈来实现,单边入栈。配对之后出栈,最后推断栈是否为空。不为空说明最后没有配对完毕。返回false.
详细代码例如以下:
public class Solution {
public boolean isValid(String s) {
Stack<Character> st = new Stack<Character>();
char[] ch = s.toCharArray();
int len = ch.length;
//假设长度为奇数,肯定无效
if((len & 1) != 0){
return false;
}
for(int i = 0; i < len; i++){
if(st.isEmpty()){//栈为空,直接入栈
st.push(ch[i]);
}else{//不为空则讨论栈顶元素是否与ch[i]配对。
配对也出栈
if(isMatch(st.peek(),ch[i])){//是否配对
st.pop();//栈顶元素出栈
}else{
st.push(ch[i]);//不配对入栈
}
}
}
return st.isEmpty();
}
//a为栈顶元素,b为字符串最前元素
public static boolean isMatch(char a, char b){
switch(a){
case '(': if(b == ')') return true; else return false;
case '{': if(b == '}') return true; else return false;
case '[': if(b == ']') return true; else return false;
default : return false;
}
}
}
leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法的更多相关文章
- [LeetCode]20. Valid Parentheses有效的括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- leetcode 20 Valid Parentheses 有效的括号
描述: 给定一些列括号,判断其有效性,即左括号有对应的有括号,括号种类只为小,中,大括号. 解决: 用栈. bool isValid(string s) { stack<char> st; ...
- leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- Java [leetcode 20]Valid Parentheses
题目描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...
- [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, 力扣 ...
随机推荐
- [Luogu 1850] noip16 换教室
[Luogu 1850] noip16 换教室 好久没有更博客了,先唠嗑一会,花了两天的空闲时间大致做完了昨年的noip真题 虽然在经过思考大部分题目都可出解(天天爱跑步除外),但是并不知道考试时候造 ...
- SQLServer2008 在where条件中使用CASE WHEN
create table #temp( id int identity(1,1), name varchar(20), startYear int, startMonth in ...
- Java基础13一异常
1.异常概念 异常是程序在运行期发生的不正常的事件,它会打断指令的正常执行流程. 设计良好的程序应该在异常发生时提供处理这些不正常事件的方法,使程序不会因为异常的发生而阻断或产生不可预见的结果. Ja ...
- 深入浅出java多态
所谓多态就是指程序中定义的引用变量所指向的具体类型和通过该引用变量发出的方法调用在编程时并不确定,而是在程序运行期间才确定,即一个引用变量倒底会指向哪个类的实例对象,该引用变量发出的方法调用到底是哪个 ...
- 终极解决VS2015 安装失败问题,如 安装包损坏或丢失
1.去微软官网下载完成ISO镜像,最好不要在线安装, 打开官方链接 https://www.visualstudio.com/zh-cn/downloads/download-visual-studi ...
- 针对Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1的解决方案
背景:本项目使用JDK1.8 编译maven工程的时候出现如下错误: Failed to execute goal org.apache.maven.plugins:maven-compiler-pl ...
- CorelDRAW图片导出变色,如何解决?
很多小伙伴反映说CDR颜色导出不准确,特别是CorelDRAW X4以及之前的版本,那么CDR导出变色的问题是怎么导致的,如何解决呢,本文小编分享一些自己的心得. 一:出现问题. 比如下面这个问题,明 ...
- springMvc学习地址新
http://www.admin10000.com/document/6436.html 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar ...
- struts 2 action result类型
最近在管理公司老项目的时候发现如下代码: <bean name="detailPlayer" class="PlayAction" method=&quo ...
- Codeforces 892C/D
C. Pride 传送门:http://codeforces.com/contest/892/problem/C 本题是一个关于序列的数学问题——最大公约数(GCD). 对于一个长度为n的序列A={a ...