leetcode—Valid Parentheses
1.问题描述
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.
2.解法分析
典型的栈用法。
class Solution {public:bool isValid(string s) {// Start typing your C/C++ solution below// DO NOT write int main() functionvector<char> myStack;map<char,char>dict;dict.insert(make_pair('(',')'));dict.insert(make_pair('[',']'));dict.insert(make_pair('{','}'));for(int i=0;i<s.length();++i){if(s[i]=='('||s[i]=='['||s[i]=='{')myStack.push_back(s[i]);elseif(s[i]==')'||s[i]==']'||s[i]=='}'){if(myStack.empty())return false;if(dict[myStack.back()]==s[i])myStack.pop_back();else return false;}}if(myStack.empty())return true;return false;}};
leetcode—Valid Parentheses的更多相关文章
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode: Valid Parentheses 解题报告
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...
- [Leetcode] valid parentheses 有效括号对
Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...
- LeetCode——Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Python3解leetcode Valid Parentheses
问题描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...
- leetcode Valid Parentheses python
# 解题思路: # 创建一个字典映射关系 dicts# 使用一个栈stk 遍历字符串s 得到一个新的字符串curItem 如果lastItem在dicts中的value和它相等 不做任何操作# 如果不 ...
- LeetCode Valid Parentheses 有效括号
class Solution { public: void push(char c){ //插入结点 struct node *n=new struct node; n->nex=; n-> ...
- Valid Parentheses [LeetCode 20]
1- 问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...
随机推荐
- 252. Meeting Rooms
题目: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] ...
- PCL—低层次视觉—点云滤波(基于点云频率)
1.点云的频率 今天在阅读分割有关的文献时,惊喜的发现,点云和图像一样,有可能也存在频率的概念.但这个概念并未在文献中出现也未被使用,谨在本博文中滥用一下“高频”一词.点云表达的是三维空间中的一种信息 ...
- opencv 画延长线
hough变换可以让我们检测到直线,这在前面已有详解,对于车道检测,我们需要其到图像边界的延长线一遍之后数据帧分析. 以下代码帮助我们在opencv中画延长线,本来想用虚线表示延长线的,无奈参数调不好 ...
- UVa 1402 Runtime Error 伸展树
Runtime Error 到现在连样例也跑不出来!!! 调试了一晚上快要死了…… 知道错在哪里但是不会改,代码先扔在这里吧.看来不能太依赖模板啊orz…… #include <cstdio&g ...
- android下activity中多个listview只允许主界面滚动
之前发现了自己的APP在处理两个listview时产生的一个bug.当两个listview中的item数量多出手机屏幕时,listview不能显示完全.一开始觉得只要加一个scrollview就可以了 ...
- web前端性能测试小点
关于前端性能的文章: http://www.cnblogs.com/fnng/archive/2011/09/19/2181894.html web应用的前端性能响应时间指浏览器的页面加载时间.浏览器 ...
- Java内部类总结 分类: 原理 2015-06-28 09:51 9人阅读 评论(0) 收藏
内部类是指在一个外部类的内部再定义一个类.内部类作为外部类的一个成员,并且依附于外部类而存在的. 内部类可为静态,可用protected和private修饰(而外部类只能使用public和缺省的包访问 ...
- RecyclerView(4)简单示例
1,RecyclerViewFrgmt import com.example.adapter.R; import android.app.Fragment; import android.os.Bun ...
- 转:Cache相关
声明:本文截取自http://blog.163.com/ac_victory/blog/static/1033187262010325113928577/ (1)“Cache”是什么 Cache(即高 ...
- Codeforces Round #207 (Div. 2)C
读错题意了..线段树延迟标记 白刷这么多线段树 #include <iostream> #include<cstdio> #include<cstring> #in ...