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. LintCode Edit Distance

    LintCode Edit Distance Given two words word1 and word2, find the minimum number of steps required to ...

  2. java虚拟机之垃圾回收算法

    标记-清除算法: 这是最基础的,就是之前所讲的两次标记,首先标记出所有 需要回收的对象,然后进行统一清除, 这有两缺点:一是效率低,标记和清除(开启低优先级进行回收)都是低效率的.第二是空间问题,标记 ...

  3. java问题排查可能用到的一些命令

    1. jmap查询jvm内存使用情况 -heap :打印jvm heap的情况 -histo: 打印jvm heap的直方图.其输出信息包括类名,对象数量,对象占用大小. -histo:live : ...

  4. debain 8为Iceweasel安装flash播放器

    到adobe官网下载flash.或https://get.adobe.com/flashplayer/?loc=cn 下载tar.gz文件后,解压缩后会有一个libflashplayer.so 文件. ...

  5. LintCode Implement Queue by Two Stacks

    1. stack(先进后出): pop 拿出并返回最后值: peek 返回最后值: push 加入新值在后面并返回此值. 2. queue(先进先出) : poll = remove 拿出并返第一个值 ...

  6. 1 error C4996: 'pcl::SAC_SAMPLE_SIZE':

    使用PCL1.8   中使用粗配准拼接 错误 1 error C4996: 'pcl::SAC_SAMPLE_SIZE': This map is deprecated and is kept onl ...

  7. Linux防火墙简介及iptables的基本使用

    一.防火墙基础知识 iptables/netfilter:网络防火墙,连接追踪(状态检测) netfilter:工作内核中,让规则能够生效的网络框架(framework) iptables:防火墙规则 ...

  8. cx_oracle 执行cur.execute(sql)提交数据出现 UnicodeEncodeError: 'ascii' codec can't encode character u'\u2122' in position 170

    还是中文字符的问题, 解决方法见链接:http://www.oracle.com/technetwork/articles/tuininga-cx-oracle-084866.html import ...

  9. 自学 PHP,如何不走弯路?

    1.一本好书至关重要.如果这本书的知识非常深入,那么还是不要看了.对初学者来说只能是打击.因为很多东西都看不懂.一本知识较为浅显,并且说明非常详细,但是能让你上手的基础知识又非常完善的书籍就非常好.( ...

  10. JSP编译原理图解