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 ...
随机推荐
- sonar runner 2.4
https://www.versioneye.com/java/org.codehaus.sonar.runner:sonar-runner-dist/2.4
- Tomcat 管理页面
一.配置刚下载的解压版的apache-tomcat,启动后,通过浏览器访问:http://127.0.0.1:8080/(或者http://localhost:8080)然后点击下图的Server s ...
- PHP 语言需要避免的 10 大误区
PHP是一种非常流行的开源服务器端脚本语言,你在万维网看到的大多数网站都是使用php开发的.但是,你大概很奇怪的注意到有少部分的人发誓要离php 远远的.但是令人更奇怪的是或者很震惊的说他们不用php ...
- (step4.3.5)hdu 1501(Zipper——DFS)
题目大意:个字符串.此题是个非常经典的dfs题. 解题思路:DFS 代码如下:有详细的注释 /* * 1501_2.cpp * * Created on: 2013年8月17日 * Author: A ...
- 分享一个免费SSL证书申请网站,给网站开启https协议 | 张戈博客
这些天,由于公司的业务需求,接触到了ssl证书和https协议.博客前几篇文章也分享了在WEB服务器上安装SSL证书,为网站开启https协议的教程,感兴趣的童鞋可以前往查看相关文章: <Lin ...
- shell/bash 让vi/vim显示空格,及tab字符
shell/bash 让vi/vim显示空格,及tab字符 Vim 可以用高亮显示空格和TAB.文件中有 TAB 键的时候,你是看不见的.要把它显示出来::set listTAB 键显示为 ^I, ...
- noi2002银河英雄传说(并查集)
首先表示对C++读入读出问题复杂程度的敬畏,看了好多没讲明白的,本题用cin竟然过不了评测,搞scanf的读入搞了好久.... 本题确实是一道经典的并查集题型,不多讲,拿来练练手用的(其中经历很惨) ...
- super.getClass()方法
下面程序的输出结果是多少? importjava.util.Date; public class Test extends Date{ public static void main(String[] ...
- 本来运行的好的Ajax.dll怎么突然不起作用了
客户中有个好多年前老项目用了Ajax.dll,前几天突然不起作用了. 问了下原因,系统从Windows Server2003 升级为 Windows Server 2008了,IIS也从6升级成7了. ...
- SQL2008 R2 主从数据库同步设置
一.准备工作: 主数据库服务器: OS:Windows Server 2008 R2 DB: SQL Server 2008 R2 Hostname : CXMasterDB IP: 192.1 ...