问题:

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.

提示:

Stack String

方法1:(效率低)

#include <stack>
#include <string>class Solution {
public:bool isValid(string s) {stack<char> mystack;for(int i=0; s[i]!='\0'; i++){char c=s[i];switch(c){case '(': case '[': case '{':mystack.push(c);break;case ')':if(mystack.empty() || mystack.top()!='(')return false;elsemystack.pop();break;case ']':if(mystack.empty() || mystack.top()!='[')return false;elsemystack.pop();break;case '}':if(mystack.empty() || mystack.top()!='{')return false;elsemystack.pop();break;}}return mystack.empty();}
};

方法2:(效率低)

#include <stack>
#include <string>class Solution {
public:bool isValid(string s) {stack<char> mystack;map<char,char> mymap;mymap.insert(pair<char,char>('(',')'));mymap.insert(pair<char,char>('[',']'));mymap.insert(pair<char,char>('{','}'));for(int i=0; s[i]!='\0'; i++){char c=s[i];switch(c){case '(': case '[': case '{':mystack.push(c);break;case ')': case ']': case '}':if(!mystack.empty()){if(c!=mymap[mystack.top()])return false;mystack.pop(); //pop()的返回值为void
                    }elsereturn false;break;}}return mystack.empty();}
};

该题别人用Java写的:https://segmentfault.com/a/1190000003481208

C++栈的申明:http://zhidao.baidu.com/link?url=DvEEXh4U47QLJnjmv6rAeMVk1V7A_5LCii42ctj8m2ehl1MepSy0w9BSWevmfuHwlstx0D60kvBNRvAoQgFtjK

C++栈操作:http://www.169it.com/article/2839007600903800247.html

C++ map操作:http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html

转载于:https://www.cnblogs.com/calvin2/p/5964633.html

LeetCode 20 Valid Parentheses (C++)相关推荐

  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. [swift] LeetCode 20. Valid Parentheses

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

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

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

  5. LeetCode Longest Valid Parentheses

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

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

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

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

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

  8. Leetcode: Longest Valid Parentheses

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

  9. LeetCode 20. Valid Parenthese

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

最新文章

  1. iOS核心动画高级技术(十四) 图像IO
  2. 没看完这11 条,别说你精通 Python 装饰器
  3. 数据结构 单链表 (C++)(转载)
  4. python 波形发生_事件与信号
  5. python课程设计矩阵对角线之和,为每个python numpy用不同的值填充矩阵对角线
  6. 今年美国什么工作最吃香?程序猿薪酬超医生
  7. 【今日CV 计算机视觉论文速览】Thu, 28 Feb 2019
  8. AFNetworking下 http 改 https后遇到出现Error Domain=NSURLErrorDomain Code=-999 已取消 错误...
  9. node.js常见问题之:解决npm install出错(Cannot find module ‘internal/util/types‘)
  10. Cors 跨域 共享
  11. java 解压rar文件
  12. 防华为手表太空人主题界面 html5 代码实现
  13. 【Unity3D插件】Alembic插件分享《abc动画文件导入Unity》
  14. 2022-03-09:我们正在玩一个猜数游戏,游戏规则如下: 我从 1 到 n 之间选择一个数字。 你来猜我选了哪个数字。 如果你猜到正确的数字,就会 赢得游戏 。 如果你猜错了,那么我会告诉你,我选
  15. 蚂蚁区块链第15课 JS SDK概述及API接口速查
  16. 飞塔防火墙密码恢复。
  17. bose蓝牙音箱使用说明_性价比甄选 推荐这5款超值得入手的蓝牙音箱
  18. JAVA实现的Johnson-trotter算法(高效的全排列算法)
  19. 什么时候要抛出异常? !
  20. Linux 压缩包乱码

热门文章

  1. JEE_Ajax技术
  2. 敏捷开发一千零一问系列之十五:同时实施CMMI和敏捷哪个为主?
  3. 某交规模拟考试系统验证绕路手记
  4. windows10下 tensorflow gpu版本安装配置方法
  5. 问题 I: 简单的整数排序
  6. python错误异常处理try except Error
  7. ios 获取是否静音模式_高效人士进阶-IOS
  8. Linux之文件系统介绍
  9. ChaiNext:大盘调整,主流币种还未稳住阻力位
  10. ChaiNext:市场短期内延续区间震荡