题目:

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)括号可嵌套,如{[()]}

实现:

  1. class Solution {
  2. public:
  3. bool isValid(string s) {
  4. if(s.empty()) return false;//空字符串,false
  5. int len=strlen(s.c_str());
  6. if(len==) return false;//只有一个字符,false
  7. char pre;
  8. stack<char> char_stack;
  9. for(int i=;i<len;i++)
  10. {
  11. if(isPop(s[i])) //该出栈顶元素
  12. {
  13. if(!char_stack.empty())
  14. {
  15. pre=char_stack.top();//取栈顶元素、并出栈
  16. char_stack.pop();
  17. if(!isChar(pre,s[i]))//是否匹配
  18. return false;
  19. }
  20. else return false;
  21. }
  22. else //入栈
  23. {
  24. char_stack.push(s[i]);
  25. }
  26. }
  27. return char_stack.empty();//栈空true,否则false
  28. }
  29. private:
  30. bool isPop(char t) //判断是否是)} ]符号,是则出栈栈顶元素
  31. {
  32. if(t==')'||t=='}'||t==']')
  33. return true;
  34. else
  35. return false;
  36. }
  37. bool isChar(char t1,char t2)//判断两字符t1,t2是否匹配
  38. {
  39. switch (t1)
  40. {
  41. case '(':
  42. {
  43. if(t2==')') return true;
  44. else return false;
  45. break;
  46. }
  47. case '{':
  48. {
  49. if(t2=='}') return true;
  50. else return false;
  51. break;
  52. }
  53. case '[':
  54. {
  55. if(t2==']') return true;
  56. else return false;
  57. break;
  58. }
  59. default:
  60. return false;
  61. }
  62. }
  63. };

leetcode题解:Valid Parentheses(栈的应用-括号匹配)的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 最长有效括号

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

  2. 32. Longest Valid Parentheses(最长括号匹配,hard)

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

  3. leetcode 题解 || Valid Parentheses 问题

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

  4. Leetcod--20. Valid Parentheses(极简洁的括号匹配)

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

  5. 栈应用之 括号匹配问题(Python 版)

    栈应用之 括号匹配问题(Python 版) 检查括号是否闭合 循序扫描被检查正文(一个字符)里的一个个字符 检查中跳过无关字符(所有非括号字符都与当前处理无关) 遇到开括号将其压入栈 遇到闭括号时弹出 ...

  6. [Leetcode] longest valid parentheses 最长的有效括号

    Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...

  7. [LeetCode] 32. Longest Valid Parentheses 最长有效括号

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

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

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

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

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

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

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

随机推荐

  1. P4397 [JLOI2014]聪明的燕姿

    P4397 [JLOI2014]聪明的燕姿 题目背景 阴天傍晚车窗外 未来有一个人在等待 向左向右向前看 爱要拐几个弯才来 我遇见谁会有怎样的对白 我等的人他在多远的未来 我听见风来自地铁和人海 我排 ...

  2. FOJ ——Problem 1759 Super A^B mod C

     Problem 1759 Super A^B mod C Accept: 1368    Submit: 4639Time Limit: 1000 mSec    Memory Limit : 32 ...

  3. Linux下kill命令

    首先了解什么是信号:信号是进程级的中断请求,系统定义了30余种信号,kill是管理员用来发送信号的一种手段. 功能说明:删除执行中的程序或工作. 语 法:kill [-s <信息名称或编号> ...

  4. POJ 2676 数独+dfs深搜

    数独 #include "cstdio" #include "cstring" #include "cstdlib" #include &q ...

  5. Runtime.getRuntime().exec方法

    Runtime.getRuntime().exec()方法主要用于执行外部的程序或命令. Runtime.getRuntime().exec共有六个重载方法: public Process exec( ...

  6. JSP中的:request.getScheme()+"://"+request.getServerName()+":"+request.getServer

    String path = request.getContextPath();  String basePath = request.getScheme()+"://"+reque ...

  7. MAC删除目录下的“.svn”文件的方法

    http://bbs.feng.com/read-htm-tid-7803070.html MAC删除目录的“.svn”文件:打开终端,进到所在的目录,然后出入一下代码find . -name &qu ...

  8. Curl请求方法封装

    /** * GET请求 * @param $url * @return bool|mixed */ function http_get($url) { $oCurl = curl_init (); i ...

  9. 利用NPOI组件产Excel完整操作

    最终还是要使用NPOi了.刚开始做的是用com组件,发现如果本机不按照excel就不能使用,后来把其中一支改为了用Itextsharp产生pdf,但是还有几支批次要产生Excel,只能改用NPOI了. ...

  10. Js 利用正则表达式和replace函数获取string中所有被匹配到的文本

    js的replace函数除了替换文本以外还有获取所有被正则表达式匹配到的文本的功能.这里以一个简单的案例来作为演示. 利用正则查找出所有被两个花括号包裹的字符串: var str = '<div ...