Leetcode 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.
二、题目分析:
1.符合题意的形式还有:嵌套形式:{ [ ( ) ] } ,{ ( [] ) [ ] }
2.本题的经典在使用了 栈 的数据结构,先进后出,内层括号首先匹配,外层再进行匹配。
3.解题思路:
遍历所有输入的全部括号,
若是左括号,则压入栈中。
若是右括号,则和栈顶元素进行匹配。匹配成功则,则弹出栈顶元素,否则返回false,程序结束。
三、代码:
public class Solution {public boolean isValid(String s) {Stack<Character> stack=new Stack<Character>();for(int i=0;i<s.length();i++){char c=s.charAt(i);if(c!='}' && c!=']' && c!=')'){stack.push(c);}else{if(stack.isEmpty())return false; char topChar=stack.peek();switch(c){case ')':if(topChar=='('){stack.pop();}else{return false;}break;case ']':if(topChar=='['){stack.pop();}else{return false;}break; case '}':if(topChar=='{'){stack.pop();}else{return false;}break;}}}if(stack.isEmpty()){return true;}else{return false;}} }
转载于:https://www.cnblogs.com/lyr2015/p/6340361.html
Leetcode 20. Valid Parentheses相关推荐
- LeetCode: 20. Valid Parentheses
0509第1题(虽然是08做的,但这会已经09了) 题目 Given a string containing just the characters '(', ')', '{', '}', '[' a ...
- [LeetCode]--20. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 20 Valid Parentheses (C++)
问题: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- [swift] LeetCode 20. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 20. Valid Parentheses(c++)
利用栈的操作,遇到"(","[","{"即进栈,遇到")","]","}"判断是 ...
- LeetCode Longest Valid Parentheses
原题链接在这里:https://leetcode.com/problems/longest-valid-parentheses/ 题目: Given a string containing just ...
- LeetCode 20. Valid Parentheses--笔试题--Python解法
题目地址:Valid Parentheses - LeetCode Given a string containing just the characters '(', ')', '{', '}', ...
- 最长有效括号子串长度 c语言,LeetCode: Longest Valid Parentheses (求最长有效匹配括号子串的长度)...
题目描述: Given a string containing just the characters'(' and')', find the length of the longest valid ...
- Leetcode: Longest Valid Parentheses
Question Given a string containing just the characters '(' and ')', find the length of the longest v ...
- LeetCode 20. Valid Parenthese
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
最新文章
- 《LeetCode力扣练习》第56题 合并区间 Java
- 解读zookeeper的配置项
- 90后,一个即将成为程序员的我
- Java的主要就业方向
- WebSocket详解(一):初步认识WebSocket技术
- 2008 Gartner ITxpo 展 10 大 IT 产品
- git clone指定分支到本地
- 无约而来WIN8 PRO WMC X64 201306
- 免费MD5破解、在线查询网站
- 第一章 计算机网络概述(计算机网络韩立刚)
- libcef(一)编译CEF
- 璞华PLM为全场景产品生命周期管理赋能,助力产品主线的企业数字化转型
- redis实现的分布式锁为啥要设置过期时间?
- 开关switch系列:android Switch显示文字
- 自学Java day53 使用jvav实现 并查集 数据结构 从jvav到架构师
- 手机淘宝构架演化实践及优化,天猫,淘宝服务
- FusionCharts Free
- 【面朝大厂】面试官:说几种常用的分布式 ID 解决方案
- ChatGPT:新晋CV工程师
- 联想服务器sr650硬件配置表,ThinkSystem SR650规格解读_联想 System x3650 M4_服务器x86服务器-中关村在线...
热门文章
- warning C4996 sprintf This function or variable may be unsafe
- LINUX SHELL脚本多行注释
- 管理感悟:就事不论事
- Bridge(桥模式)
- RESTful Connector
- latex 调整表格的行高_latex 表格如何控制行高,行距,行与行之间的距离
- sketch软件_UI设计师必定需要了解的sketch素材
- 有效预防xss_4类防御XSS的有效方法
- 《一秒学会C++》异步回调函数(C++11)
- 浮点错误是什么意思_Excel函数计算常见错误值,都是什么意思