一、题目要求:

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相关推荐

  1. LeetCode: 20. Valid Parentheses

    0509第1题(虽然是08做的,但这会已经09了) 题目 Given a string containing just the characters '(', ')', '{', '}', '[' a ...

  2. [LeetCode]--20. Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  3. LeetCode 20 Valid Parentheses (C++)

    问题: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  4. [swift] LeetCode 20. Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  5. LeetCode 20. Valid Parentheses(c++)

    利用栈的操作,遇到"(","[","{"即进栈,遇到")","]","}"判断是 ...

  6. LeetCode Longest Valid Parentheses

    原题链接在这里:https://leetcode.com/problems/longest-valid-parentheses/ 题目: Given a string containing just ...

  7. LeetCode 20. Valid Parentheses--笔试题--Python解法

    题目地址:Valid Parentheses - LeetCode Given a string containing just the characters '(', ')', '{', '}', ...

  8. 最长有效括号子串长度 c语言,LeetCode: Longest Valid Parentheses (求最长有效匹配括号子串的长度)...

    题目描述: Given a string containing just the characters'(' and')', find the length of the longest valid ...

  9. Leetcode: Longest Valid Parentheses

    Question Given a string containing just the characters '(' and ')', find the length of the longest v ...

  10. LeetCode 20. Valid Parenthese

    题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

最新文章

  1. 《LeetCode力扣练习》第56题 合并区间 Java
  2. 解读zookeeper的配置项
  3. 90后,一个即将成为程序员的我
  4. Java的主要就业方向
  5. WebSocket详解(一):初步认识WebSocket技术
  6. 2008 Gartner ITxpo 展 10 大 IT 产品
  7. git clone指定分支到本地
  8. 无约而来WIN8 PRO WMC X64 201306
  9. 免费MD5破解、在线查询网站
  10. 第一章 计算机网络概述(计算机网络韩立刚)
  11. libcef(一)编译CEF
  12. 璞华PLM为全场景产品生命周期管理赋能,助力产品主线的企业数字化转型
  13. redis实现的分布式锁为啥要设置过期时间?
  14. 开关switch系列:android Switch显示文字
  15. 自学Java day53 使用jvav实现 并查集 数据结构 从jvav到架构师
  16. 手机淘宝构架演化实践及优化,天猫,淘宝服务
  17. FusionCharts Free
  18. 【面朝大厂】面试官:说几种常用的分布式 ID 解决方案
  19. ChatGPT:新晋CV工程师
  20. 联想服务器sr650硬件配置表,ThinkSystem SR650规格解读_联想 System x3650 M4_服务器x86服务器-中关村在线...

热门文章

  1. warning C4996 sprintf This function or variable may be unsafe
  2. LINUX SHELL脚本多行注释
  3. 管理感悟:就事不论事
  4. Bridge(桥模式)
  5. RESTful Connector
  6. latex 调整表格的行高_latex 表格如何控制行高,行距,行与行之间的距离
  7. sketch软件_UI设计师必定需要了解的sketch素材
  8. 有效预防xss_4类防御XSS的有效方法
  9. 《一秒学会C++》异步回调函数(C++11)
  10. 浮点错误是什么意思_Excel函数计算常见错误值,都是什么意思