Leetcod--20. Valid Parentheses(极简洁的括号匹配)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example 1:
Input: "()"
Output: true
Example 2:
Input: "()[]{}"
Output: true
Example 3:
Input: "(]"
Output: false 正常的代码
class Solution {
public boolean isValid(String s) {
Stack<Character> st=new Stack<Character>();
for(char c:s.toCharArray()){
if(c=='('||c=='['||c=='{')
st.push(c);
else if(c=='}'&&!st.empty()&&st.peek()=='{')
st.pop();
else if(c==']'&&!st.empty()&&st.peek()=='[')
st.pop();
else if(c==')'&&!st.empty()&&st.peek()=='(')
st.pop();
else
return false;
}
return st.empty();
}
}
很巧妙的方法
class Solution {
public boolean isValid(String s) {
Stack<Character> st=new Stack<Character>();
for(char c:s.toCharArray()){
if(c=='(')
st.push(')');
else if(c=='[')
st.push(']');
else if(c=='{')
st.push('}');
else if(st.empty()||st.pop()!=c)
return false;
}
if(st.empty())
return true;
else
return false;
}
}
Leetcod--20. Valid Parentheses(极简洁的括号匹配)的更多相关文章
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 20. Valid Parentheses[E]有效的括号
题目 Given a string containing just the characters '(',')','[',']','{' and '}',determine if the input ...
- 32. Longest Valid Parentheses(最长括号匹配,hard)
Given a string containing just the characters '(' and ')', find the length of the longest valid (w ...
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- leetCode练题——20. Valid Parentheses
1.题目 20. Valid Parentheses——Easy Given a string containing just the characters '(', ')', '{', '}', ...
- 刷题20. Valid Parentheses
一.题目说明 这个题目是20. Valid Parentheses,简单来说就是括号匹配.在学数据结构的时候,用栈可以解决.题目难度是Medium. 二.我的解答 栈涉及的内容不多,push.pop. ...
- 20. Valid Parentheses【leetcode】
20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
随机推荐
- Javascript 四种输出方式
JavaScript 输出 javascript 没有任何打印或输出的函数 可以通过不同的方式输出数据 使用window.alert() 弹出警告框 使用document.write()方法将内容写到 ...
- python 数据类型 之 集合
集合是一个数学概念:由一个或多个确定的元素所构成的整体叫做集合 集合的三个特性: 1.确定性 (element必须可hash,不可变类型是可hash的) 2.互异性(集合中element 不能重复) ...
- 12.Mysql存储过程和函数
12.存储过程和函数12.1 什么是存储过程和函数存储过程和函数是事先经过编译并存储在数据库中的一段SQL语句的集合,调用存储过程和函数简化应用开发人员的工作,减少数据在数据库和应用服务器之间的传输, ...
- 1S - 平方和与立方和
给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和. Input 输入数据包含多组测试实例,每组测试实例包含一行,由两个整数m和n组成. Output 对于每组输入数据,输出一行,应 ...
- 冒泡排序(js版)
基本思想:两两比较相邻记录的关键字,如果反序则交换,直至没有反序为止. 最初的冒泡排序(初级版): //从小到大 function BubbleSort(arr){ var i,j,temp; for ...
- linux RCU锁机制分析
openVswitch(OVS)源代码之linux RCU锁机制分析 分类: linux内核 | 标签: 云计算,openVswitch,linux内核,RCU锁机制 | 作者: yuzhih ...
- 转录本组装软件StringTie的使用说明
转录本组装软件StringTie的使用说明 StringTie 转录本组装软件StringTie的使用说明 转录组分析流程 HISTA + StringTie 组合.其Protocol 发表在Natu ...
- 关于nodejs 假设httpserver,会发现一次网页打开,服务端会响应两次的问题;
转自:http://cnodejs.org/topic/518772806d38277306804020 每个页面默认都会再发一个de style="line-height: 21px; p ...
- POJ 3469.Dual Core CPU 最大流dinic算法模板
Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 24830 Accepted: 10756 ...
- 【算法】Escape
The students of the HEU are maneuvering for their military training. The red army and the blue army ...