LeetCode 20 Valid Parentheses (C++)
问题:
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++)相关推荐
- LeetCode: 20. Valid Parentheses
0509第1题(虽然是08做的,但这会已经09了) 题目 Given a string containing just the characters '(', ')', '{', '}', '[' a ...
- [LeetCode]--20. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [swift] LeetCode 20. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 20. Valid Parentheses(c++)
利用栈的操作,遇到"(","[","{"即进栈,遇到")","]","}"判断是 ...
- LeetCode Longest Valid Parentheses
原题链接在这里:https://leetcode.com/problems/longest-valid-parentheses/ 题目: Given a string containing just ...
- LeetCode 20. Valid Parentheses--笔试题--Python解法
题目地址:Valid Parentheses - LeetCode Given a string containing just the characters '(', ')', '{', '}', ...
- 最长有效括号子串长度 c语言,LeetCode: Longest Valid Parentheses (求最长有效匹配括号子串的长度)...
题目描述: Given a string containing just the characters'(' and')', find the length of the longest valid ...
- Leetcode: Longest Valid Parentheses
Question Given a string containing just the characters '(' and ')', find the length of the longest v ...
- LeetCode 20. Valid Parenthese
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
最新文章
- iOS核心动画高级技术(十四) 图像IO
- 没看完这11 条,别说你精通 Python 装饰器
- 数据结构 单链表 (C++)(转载)
- python 波形发生_事件与信号
- python课程设计矩阵对角线之和,为每个python numpy用不同的值填充矩阵对角线
- 今年美国什么工作最吃香?程序猿薪酬超医生
- 【今日CV 计算机视觉论文速览】Thu, 28 Feb 2019
- AFNetworking下 http 改 https后遇到出现Error Domain=NSURLErrorDomain Code=-999 已取消 错误...
- node.js常见问题之:解决npm install出错(Cannot find module ‘internal/util/types‘)
- Cors 跨域 共享
- java 解压rar文件
- 防华为手表太空人主题界面 html5 代码实现
- 【Unity3D插件】Alembic插件分享《abc动画文件导入Unity》
- 2022-03-09:我们正在玩一个猜数游戏,游戏规则如下: 我从 1 到 n 之间选择一个数字。 你来猜我选了哪个数字。 如果你猜到正确的数字,就会 赢得游戏 。 如果你猜错了,那么我会告诉你,我选
- 蚂蚁区块链第15课 JS SDK概述及API接口速查
- 飞塔防火墙密码恢复。
- bose蓝牙音箱使用说明_性价比甄选 推荐这5款超值得入手的蓝牙音箱
- JAVA实现的Johnson-trotter算法(高效的全排列算法)
- 什么时候要抛出异常? !
- Linux 压缩包乱码