LeetCode(20)Valid Parentheses
题目
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代码
class Solution {
public:
    bool isValid(string s) {
        int len = strlen(s.c_str());
        if (len == 0)
            return true;
        if (len % 2 != 0)
            return false;
        //括号匹配问题,用数据结构栈辅助实现
        stack<char> sta;
        for (int i = 0; i < len; i++)
        {
            if (s[i] != ')' && s[i] != ']' && s[i] != '}')
                sta.push(s[i]);
            else
            {
                if (sta.size() <= 0)
                    return false;
                if (s[i] == PairLetter(sta.top()))
                    sta.pop();
                else
                    return false;
            }//else
        }//for
        if (sta.size() == 0)
            return true;
        else
            return false;
    }
    char PairLetter(const char &c)
    {
        switch (c)
        {
        case '(':
            return ')'; break;
        case '[':
            return ']'; break;
        case '{':
            return '}'; break;
        default:
            return 0; break;
        }
    }
};LeetCode(20)Valid Parentheses的更多相关文章
- LeetCode(49)-Valid Parentheses
		题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ... 
- 【LeetCode算法-20】Valid Parentheses
		LeetCode第20题 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determin ... 
- LeetCode(36)Valid Sudoku
		题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ... 
- LeetCode(242)Valid Anagram
		题目 Given two strings s and t, write a function to determine if t is an anagram of s. For example, s ... 
- leetcode第20题--Valid Parentheses
		Problem: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ... 
- LeetCode(20):有效的括号
		Easy! 题目描述: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭 ... 
- LeetCode(125) Valid Palindrome
		题目 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ign ... 
- LeetCode(22)Generate Parentheses
		题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ... 
- LeetCode(65) Valid Number
		题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ... 
随机推荐
- Asp.net core 框架整理
			https://github.com/thangchung/awesome-dotnet-core#cms 
- 跟我一起玩Win32开发(16):ListView的多个视图
			在上一个例子中,我们只用到了ListView的Report视图,也就是详细视图.本文我们再把上一篇文章中所用的例子进行一下扩展,例子源码可以到俺的资源区下载. 我们为ListView中显示的数据加上图 ... 
- Linux源码编译处理
			1. 解决依赖问题 查询需要的依赖软件,提前安装好若使用命令行安装,一般使用默认路径:使用源码安装,则自定义安装路径,后续可能需要进行路径配置PS:可能需要在Makefile等配置文件中添加相关库文件 ... 
- ashx 中获取 session获取信息
			1.在应用程序中获取session,System.Web.HttpContext.Current.Session: 2.命名空间如下:IRequiresSessionState 调用方法 public ... 
- GCD Counting Codeforces - 990G
			https://www.luogu.org/problemnew/show/CF990G 耶,又一道好题被我浪费掉了,不会做.. 显然可以反演,在这之前只需对于每个i,统计出有多少(x,y),满足x到 ... 
- 139 Word Break 单词拆分
			给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,确定 s 是否可以被空格分割为一个或多个在字典里出现的单词.你可以假设字典中无重复的单词.例如,给出s = "leet ... 
- 纯js手动分页
			昨天让做个页面,后台提供所有数据,没有做好分页,需要前端js手动分页. 我参考了 http://www.cnblogs.com/jiechn/p/4095029.html 做了些许改动让分页效果更加完 ... 
- azkaban web ui界面出现异常诡异“丑”界面的问题解决(图文详解)
			前期博客 启动azkaban时出现User xml file conf/azkaban-users.xml doesn't exist问题解决(图文详解) 问题详情 [hadoop@master co ... 
- Suricata的输出
			不多说,直接上干货! 见官网 https://suricata.readthedocs.io/en/latest/output/index.html 总的来说,Suricata采集下来的数据输出分为: ... 
- 【转】10种简单的Java性能优化
			10种简单的Java性能优化 2015/06/23 | 分类: 基础技术 | 14 条评论 | 标签: 性能优化 分享到: 本文由 ImportNew - 一直在路上 翻译自 jaxenter.欢迎加 ... 
