Generate 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。

思路:可用于卡特兰数一类题目。

  1. void getParenthesis(vector<string> &vec, string s, int left, int right) {
  2. if(!right && !left) { vec.push_back(s); return; }
  3. if(left > 0)
  4. getParenthesis(vec, s+"(", left-1, right);
  5. if(right > 0 && left < right)
  6. getParenthesis(vec, s+")", left, right-1);
  7. }
  8.  
  9. class Solution {
  10. public:
  11. vector<string> generateParenthesis(int n) {
  12. vector<string> vec;
  13. getParenthesis(vec, string(), n, n);
  14. return vec;
  15. }
  16. };

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.

思路: 栈。对 S 的每个字符检查栈尾,若成对,则出栈,否则,入栈。

  1. class Solution {
  2. public:
  3. bool isValid(string s) {
  4. bool ans = true;
  5. char ch[6] = {'(', '{', '[', ']', '}', ')'};
  6. int hash[256] = {0};
  7. for(int i = 0; i < 6; ++i) hash[ch[i]] = i;
  8. string s2;
  9. for(size_t i = 0; i < s.size(); ++i) {
  10. if(s2 != "" && hash[s2.back()] + hash[s[i]] == 5) s2.pop_back();
  11. else s2.push_back(s[i]);
  12. }
  13. if(s2 != "") ans = false;
  14. return ans;
  15. }
  16. };

72. Generate Parentheses && Valid Parentheses的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. [LeetCode] 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

  3. [LeetCode] 32. Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  4. [LeetCode] Valid Parentheses 验证括号

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

  5. lettcode笔记--Valid Parentheses

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

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

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

  7. [LeetCode] 20. Valid Parentheses 验证括号

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

  8. [LeetCode] 20. Valid Parentheses 合法括号

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

  9. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

随机推荐

  1. QT5学习过程的小问题集锦

    *** only available with -std=c++11 or -std=gnu++11 添加以下代码到*.pro文件. CONFIG += c++11 在 Qt creator 中设置 ...

  2. asd

    弹出输入提示框 :window.prompt(); 24. 指定当前显示链接的位置 :window.location.href="URL" 25. 取出窗体中的所有表单的数量 :d ...

  3. 转载《android:scaleType属性》

    在网上查了好多资料,大致都雷同,大家都是互相抄袭的,看着很费劲,不好理解,自己总结一下,留着需要看的话来查找. 代码中的例子如下: <ImageView android:id="@+i ...

  4. Python通用序列操作

    1.序列概览 1.数据结构 序列.容器 Python中最基本的数据结构是序列,其有索引(从左到右第一个索引为0,从右到左第一个索引为-1). Python包含6中内建的序列: 列表 元组 字符串 Un ...

  5. Beta版本测试报告

    爬虫测试: 由于爬虫是整个系统的数据来源,十分的重要,但是由于引用了jar包并且运行复杂,这里主要做功能性测试,通过增加seed,运行爬虫,可以在后台控制台看到日志的不断刷新以及数据库条目的不断增加, ...

  6. 【我是老中医】VMware在win8.1下开Ubuntu提示”内部错误"解决方案

    这个题目起得很洋气啊,其实问题也比较好解决,但是我想多码几个字!!! 友情提示:如果不想看废话,请直接看最后的红字! 好的,咱们从头说(废话)起.话说我们学院每年都会组织大三的进行校企联合实训(其实就 ...

  7. 免费SSL-HTTS 申请与配置 NGINX配置

    Let's Encrypt是很火的一个免费SSL证书发行项目,自动化发行证书,证书有90天的有效期.适合个人使用或者临时使用,不用再忍受自签发证书不受浏览器信赖的提示.Let's Encrypt已经发 ...

  8. 简述jsp之EL表达式和jstl及其使用

    Jsp的指令之include指令include指令:代表的是页面的包含. 作用:可以把一些jsp的页面包含在一起,对外展示. 页面的布局,现在已经不用了,现在都用css+div进行布局.include ...

  9. IOS开发之--NSPredicate

    我把常用的NSPredicate使用场景整理了一下   官方参考: https://developer.apple.com/library/mac/#documentation/Cocoa/Refer ...

  10. user.sh

    #!/bin/bash n=1 while [ $n -le 5 ] do n=$(( $n + 1 )) user=user$n userdel -r $user echo "$user ...