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.

详细代码例如以下:

  1. public class Solution {
  2. public boolean isValid(String s) {
  3. Stack<Character> st = new Stack<Character>();
  4. char[] ch = s.toCharArray();
  5. int len = ch.length;
  6. //假设长度为奇数,肯定无效
  7. if((len & 1) != 0){
  8. return false;
  9. }
  10.  
  11. for(int i = 0; i < len; i++){
  12. if(st.isEmpty()){//栈为空,直接入栈
  13. st.push(ch[i]);
  14. }else{//不为空则讨论栈顶元素是否与ch[i]配对。
  15.  
  16. 配对也出栈
  17. if(isMatch(st.peek(),ch[i])){//是否配对
  18. st.pop();//栈顶元素出栈
  19. }else{
  20. st.push(ch[i]);//不配对入栈
  21. }
  22. }
  23. }
  24. return st.isEmpty();
  25. }
  26. //a为栈顶元素,b为字符串最前元素
  27. public static boolean isMatch(char a, char b){
  28. switch(a){
  29. case '(': if(b == ')') return true; else return false;
  30. case '{': if(b == '}') return true; else return false;
  31. case '[': if(b == ']') return true; else return false;
  32. default : return false;
  33. }
  34. }
  35. }

leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法的更多相关文章

  1. [LeetCode]20. Valid Parentheses有效的括号

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

  2. leetcode 20 Valid Parentheses 有效的括号

    描述: 给定一些列括号,判断其有效性,即左括号有对应的有括号,括号种类只为小,中,大括号. 解决: 用栈. bool isValid(string s) { stack<char> st; ...

  3. leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  4. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  5. Java [leetcode 20]Valid Parentheses

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

  6. [LeetCode] 20. Valid Parentheses 验证括号

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

  7. [LeetCode] 20. Valid Parentheses 合法括号

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

  8. [leetcode]20. Valid Parentheses有效括号序列

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

  9. 【LeetCode】20. Valid Parentheses 有效的括号

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...

随机推荐

  1. yaml标记语言的简介

    今天遇到yml这个文件,挺懵的.也是百度了一把. 这篇博文不错:http://www.ibm.com/developerworks/cn/xml/x-1103linrr/ 这图画得不错:http:// ...

  2. Excel 批量出来数据

    try { string sheetname = TextBox1.Text.Trim(); HttpPostedFile upLoadPostFile = FileUpload1.PostedFil ...

  3. Caffe+Kubuntu16.04_X64+CUDA 8.0配置

    前言: 经过尝试过几次Caffe,theano,MxNet之后,很长时间没有进行caffe的更新,此次在Ubuntu16.04下安装Caffe,折腾了一天时间,终于安装成功. 参考链接:Caffe+U ...

  4. C#—接口和抽象类的区别?

    一.接口 接口是指对协定进行定义的引用类型,其他类型实现接口,以保证它们支持某些操作.接口指定必须由类提供的成员或实现它的其他接口.与类相似,接口可以包含方法.属性.索引器和事件作为成员. 1.接口存 ...

  5. 从输入url到页面展示出来经历了哪些过程

    本文只是一个整理向的随笔,以个人思路来简化的同时进行适当的拓展,如有错误,欢迎指正. 1.输入网址.  此时得到一个url 2.域名解析 整个过程都是dns系统在发挥作用,它的目的是将域名和ip对应起 ...

  6. windows server 2008如何显示后缀名

    任意打开一个文件----点击左上角有个[组织]---[文件夹和搜索选项]---[查看]----去掉勾[隐藏已知文件类型的扩展名]---确定即可

  7. Uoj 52. 【UR #4】元旦激光炮 神题+交互题

    Code: #include "kth.h" #include<iostream> int minn(int x,int y){return x<y?x:y;}; ...

  8. 单调队列 && 单调栈

    单调队列 && 单调栈 单调队列 维护某个滑动区间的min or max,可用于dp的优化 以维护min为例,采用STL双端队列实现 每次加入元素x前 先检查队首元素==滑动后要删除的 ...

  9. Bootstrap 表单控件一(单行输入框input,下拉选择框select ,文本域textarea)

    单行输入框,常见的文本输入框,也就是input的type属性值为text.在Bootstrap中使用input时也必须添加type类型,如果没有指定type类型,将无法得到正确的样式,因为Bootst ...

  10. jQuery Webcam Plugin jscam.swf文件反编译工具使用说明

    jQuery webcam plugin是一个在ie,firefox,chrome下都可以用的摄像头摄像及拍照用的插件. (http://www.xarg.org/project/jquery-web ...