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,问题在于取top和pop的时候忘了做异常判断,切记切记。

此外找conterpart也许是个比较头疼的问题,但是用hashmap就方便多了

map<char, char> conterpart;

bool isValid(string s) {
conterpart['('] = ')';
conterpart['['] = ']';
conterpart['{'] = '}'; if (s.empty()) return true;
stack<char> st;
for (int i = ; i < s.size(); i++) {
char c = s.at(i);
if (c == '(' || c == '[' || c == '{') {
st.push(c);
}
else if (c == ')' || c == ']' || c == '}'){
if (st.empty()) return false;
if ( conterpart[st.top()] != c) {
return false;
}
else {
st.pop();
}
}
} if (st.empty()) {
return true;
}
return false;
}

[LeetCode] Valid Parentheses的更多相关文章

  1. [LeetCode] Valid Parentheses 验证括号

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

  2. LeetCode: Valid Parentheses 解题报告

    Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...

  3. [Leetcode] valid parentheses 有效括号对

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

  4. LeetCode——Valid Parentheses

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

  5. leetcode—Valid Parentheses

    1.问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if t ...

  6. Python3解leetcode Valid Parentheses

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

  7. leetcode Valid Parentheses python

    # 解题思路: # 创建一个字典映射关系 dicts# 使用一个栈stk 遍历字符串s 得到一个新的字符串curItem 如果lastItem在dicts中的value和它相等 不做任何操作# 如果不 ...

  8. LeetCode Valid Parentheses 有效括号

    class Solution { public: void push(char c){ //插入结点 struct node *n=new struct node; n->nex=; n-> ...

  9. Valid Parentheses [LeetCode 20]

    1- 问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...

随机推荐

  1. Python自动化之配置Python模块国内镜像

    Linux环境 在~/.pip/pip.conf文件中添加或修改 [global] index-url = http://mirrors.aliyun.com/pypi/simple/ [instal ...

  2. @PathVariable注解

    spring mvc中的@PathVariable是用来获得请求url中的动态参数的,十分方便,复习下: . @Controller public class TestController { @Re ...

  3. PyQt4多线程定时刷新控件

    1.通过事件关联和线程关联的方法刷新控件 self.listview=updatelistview()self.listview.updateText.connect(self.viewlist)   ...

  4. Adapter

    11-25 16:09:10.965 22791-22823/myapplication.com.myblue E/HAL: hw_get_module_by_class: lib loaded: / ...

  5. Java for LeetCode 230 Kth Smallest Element in a BST

    解题思路: 直接修改中序遍历函数即可,JAVA实现如下: int res = 0; int k = 0; public int kthSmallest(TreeNode root, int k) { ...

  6. vs版本转换工具

      [转]C#写的工程项目移植转换工具 – 支持VS2005/VS2010/VS2012/VS2013 经常用Visual Studio开发项目的是不是会经常遇到下面这种情况或者类似于这样的情况?用新 ...

  7. Greedy:Jessica's Reading Problem(POJ 3320)

    Jessica's Reading Problem 题目大意:Jessica期末考试临时抱佛脚想读一本书把知识点掌握,但是知识点很多,而且很多都是重复的,她想读最少的连续的页数把知识点全部掌握(知识点 ...

  8. linux下mysql开启关和重启

    开启: /etc/init.d/mysql start关闭: /etc/init.d/mysql stop重启: /etc/init.d/mysql restart 查看字符集show variabl ...

  9. yii 多模板

    main.php: //替换所有模板 //加载文件名为first的模板 //       'theme'=>'theme1', 'components'=>array(           ...

  10. 25个增强iOS应用程序性能的提示和技巧(中级篇)(2)

    25个增强iOS应用程序性能的提示和技巧(中级篇)(2) 2013-04-16 14:42 破船之家 beyondvincent 字号:T | T 本文收集了25个关于可以提升程序性能的提示和技巧,分 ...