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。

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

void getParenthesis(vector<string> &vec, string s, int left, int right) {
if(!right && !left) { vec.push_back(s); return; }
if(left > 0)
getParenthesis(vec, s+"(", left-1, right);
if(right > 0 && left < right)
getParenthesis(vec, s+")", left, right-1);
} class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> vec;
getParenthesis(vec, string(), n, n);
return vec;
}
};

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 的每个字符检查栈尾,若成对,则出栈,否则,入栈。

class Solution {
public:
bool isValid(string s) {
bool ans = true;
char ch[6] = {'(', '{', '[', ']', '}', ')'};
int hash[256] = {0};
for(int i = 0; i < 6; ++i) hash[ch[i]] = i;
string s2;
for(size_t i = 0; i < s.size(); ++i) {
if(s2 != "" && hash[s2.back()] + hash[s[i]] == 5) s2.pop_back();
else s2.push_back(s[i]);
}
if(s2 != "") ans = false;
return ans;
}
};

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. 解决嵌入式linux系统下iconv库的移植问题

    一.解决问题    在arm开发板上使用framebuff,在汉字显示时,因为只有gb2312的16*16的汉字字库,而ubuntu16.04默认    的编码方式时utf-8,因此需要进行转码(ut ...

  2. uva1635

    /* 哑元 组合数的递推整除判断_________________________________________________________________________________ #i ...

  3. 【Summary】ANSYS TRANSIENT ANALYSIS

    1.4. Damping: https://www.sharcnet.ca/Software/Ansys/15.0.7/en-us/help/ans_str/Hlp_G_STR1D.html 8.7. ...

  4. LINQ 联表查询 取count 值

    linq to sql 实现左外部连接:var query=from a in A join b in B on a.ID equals b.aID into ab from a1 in ab.Def ...

  5. 关于C语言的一些trick

    很多东西已经记不起来了,想到一点写一点,碰到一点写一点,慢慢累积. 关于# #在宏定义中用于替换传入变量的字符,例如: #define whole_operation(n)  do { printf( ...

  6. [课程设计]Sprint One 总结&发表评论&团队贡献分

    一.总结 第一次冲刺结束了,总体来说我们团队还是做得不错的,完成了既定的目标,希望接下来还能保持这样的动力,fighting... ● 二.围观其他组并发表评论 ● http://www.cnblog ...

  7. VB CreateObject转C#

    C#调用方法.函数获取属性大致流程如下: System.Type oType = System.Type.GetTypeFromProgID("SomeClass"); objec ...

  8. ivy 配置 maven代理

    Ivy 是一个依赖管理工具,直观感受是其跟maven 的作用差不多:但这两个其实是不同的工具: maven 是面向整个项目的工程管理及构建工具:ivy 仅作为依赖管理工具,与ant 高度集成. 需要了 ...

  9. it市场

    SAP收购Sybase意欲何为 http://tech.diannaodian.com/dw/data/sybase/2012/0925/199681.html 德国行业管理软件解决方案提供商SAP宣 ...

  10. asp.net 各种路径

    Request.Path 是包含文件名的,而 Request.ApplicationPath 是应用程序路径,不包含文件名. 示例: Request.Path = /cftea/Default.asp ...