题目

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.

分析

典型的括号匹配问题,数据结构一书中的典型实例。其程序实现,借助栈结构是最佳方法。

AC代码

  1. class Solution {
  2. public:
  3. bool isValid(string s) {
  4. int len = strlen(s.c_str());
  5. if (len == 0)
  6. return true;
  7. if (len % 2 != 0)
  8. return false;
  9. //括号匹配问题,用数据结构栈辅助实现
  10. stack<char> sta;
  11. for (int i = 0; i < len; i++)
  12. {
  13. if (s[i] != ')' && s[i] != ']' && s[i] != '}')
  14. sta.push(s[i]);
  15. else
  16. {
  17. if (sta.size() <= 0)
  18. return false;
  19. if (s[i] == PairLetter(sta.top()))
  20. sta.pop();
  21. else
  22. return false;
  23. }//else
  24. }//for
  25. if (sta.size() == 0)
  26. return true;
  27. else
  28. return false;
  29. }
  30. char PairLetter(const char &c)
  31. {
  32. switch (c)
  33. {
  34. case '(':
  35. return ')'; break;
  36. case '[':
  37. return ']'; break;
  38. case '{':
  39. return '}'; break;
  40. default:
  41. return 0; break;
  42. }
  43. }
  44. };

GitHub测试程序源码

LeetCode(20)Valid Parentheses的更多相关文章

  1. LeetCode(49)-Valid Parentheses

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

  2. 【LeetCode算法-20】Valid Parentheses

    LeetCode第20题 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determin ...

  3. LeetCode(36)Valid Sudoku

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  4. LeetCode(242)Valid Anagram

    题目 Given two strings s and t, write a function to determine if t is an anagram of s. For example, s ...

  5. leetcode第20题--Valid Parentheses

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

  6. LeetCode(20):有效的括号

    Easy! 题目描述: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭 ...

  7. LeetCode(125) Valid Palindrome

    题目 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ign ...

  8. LeetCode(22)Generate Parentheses

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  9. LeetCode(65) Valid Number

    题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...

随机推荐

  1. js同过url下载文件,调用另存为弹框

    实战中,项目将文件上传到项目的根目录下,并进行保存,随后根据此文件的路径进行下载到本地磁盘 实战中,项目将文件上传到项目的根目录下,并进行保存,随后根据此文件的路径进行下载到本地磁盘 实战中,项目将文 ...

  2. VS2008/2010 都不能使用Access2010数据库

    VS2008/2010 都不能使用Access2010数据库,因为VS2008/2010都不能使用X64位的Access数据库引擎.

  3. UvaLive3942(Trie + dp)

    查了半天数组越界的RE,才发现自己把ch数组放结构体里是过大的……放全局就A了. 类似区间的dp比较显然,只是用trie树做了优化,使得可以在trie树里一边走一边往上加dp值,不必枚举以前的每个位置 ...

  4. Java 修改编码格式的几种方式

    1.工作空间 workspase Window→Preferences→General→Workspace→Text file encoding→other→UTF-8 2.项目编码格式 右键项目名→ ...

  5. Eclipse显示空白符,及使用google代码格式化

    启动Eclipse,打开Preferences对话框.菜单“window”-“Preferences”. 找到Text Editors,勾选show whitespace characters,如图: ...

  6. 使用 Realm 和 Swift 创建 ToDo 应用

    原文出处: HOSSAM GHAREEB   译文出处:Prayer’s blog(@EclipsePrayer) 智能手机的快速发展的同时,涌现出了很多对开发者友好的开发工具,这些工具不仅使得开发变 ...

  7. Java开发笔记(九十五)NIO配套的文件工具Files

    NIO不但引进了高效的文件通道,而且新增了更加好用的文件工具家族,包括路径组工具Paths.路径工具Path.文件组工具Files.先看路径组工具Paths,该工具提供了静态方法get,输入某个文件的 ...

  8. iOS 从相册中拿到 图片名 ,截取后缀,图片名

    //从路径中获得完整的文件名 (带后缀) 对从相册中取出的图片,视频都有效. NSString *fileName = [filePath lastPathComponent]; //获得文件名 (不 ...

  9. C# 图片打印杂谈

    日常开头水一下,看了下上次博客,一年零八天了,啧啧,奢侈. 最近这个工作挺满意的,是我想要的发展方向,后续要做机器学习,现在得先把公司之前堆积的问题解决了. 谈人生到此结束,还是说正题吧.(感觉这标题 ...

  10. Unity c# 状态机的简单入门

    状态机模式在unity中作用是非常大的,可以实现角色的移动和场景的跳转,包括一些动画的播放,在很多unity框架中也是很常见的,发散思维广阔,下面是简单的状态机的实现,有注释 using System ...