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 ']', ...
随机推荐
- andorid 网格视图GridView
.xml <?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android=&qu ...
- 最大子序列(java版)
package com.algorithm.test; /** * 最大子序列 * @author LiFen * */ public class LargestSubsequence { publi ...
- 大数乘法java版
import java.util.*; import java.math.*; public class NumMul{ public static void main(String args[]){ ...
- 如何使用NSDL玩转微信跳一跳
目前网上介绍windows和IOS操作系统上玩微信跳一跳的有很多文章,但介绍Linux平台下的文章相对较少,所以动手操作下和大家分享,同时感谢wangshub在github上的分享: 1 下载wech ...
- Telnet远程登录
假设 电脑A Telnet远程登录 电脑B (Windows) 1.电脑B: 关闭防火墙 开启Telnet服务:“我的电脑”-->“管理”-->“服务”-->Telnet开启 2.电 ...
- UI设计教程学习分享:APP布局
一.宫格布局 这种APP信息布局方式也是我们目前最常见的一种方式,也是符合用户习惯和黄金比例的设计方式,最知名的就是锤子手机的界面设计.锤子手机界面设计欣赏知名的APP设计采用的九宫格.六宫格等方式布 ...
- 52ABP视频学习
https://study.163.com/course/courseMain.htm?courseId=1005208064 网易视频 https://www.52abp.com/ReadWiki/ ...
- Best Sightseeing Pair LT1014
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and t ...
- python输出格式对齐问题
采用.format打印输出时,可以定义输出字符串的输出宽度,在 ':' 后传入一个整数, 可以保证该域至少有这么多的宽度. 用于美化表格时很有用. >>> table = {'Goo ...
- Java学习笔记:多线程(一)
Java中线程的五种状态: 新建状态(New) 就绪状态(Runnable) 运行状态(Running) 阻塞状态(Blocked) 凋亡状态(Dead) 其中阻塞状态(Blocked)又分为三种: ...