Generate Parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

"((()))", "(()())", "(())()", "()(())", "()()()"

只要左括号数大于1就可以添加左括号。只要右括号数大于左括号数就可以添加右括号。

 class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> res; recursive(n, n, "", res); return res;
} void recursive(int n1, int n2, string str, vector<string> &res) {
if (n1 == && n2 == ) {
res.push_back(str);
return;
} if (n1 >= ) {
recursive(n1 - , n2, str + "(", res);
} if (n2 > n1) {
recursive(n1, n2 - , str + ")", res);
}
}
};

网上查了一下,竟然还和Catalan数有关。

通项公式是: \(\frac{(2n)!}{(n+1)!n!}\)

递推公式是 \(C_0=1\ and\ C_{n+1}=\sum\limits^n_{i=0}{C_{i}C_{n-i}}\)

n个+1和n个-1构成2n项\(a_1,a_2,\ldots,a_n\),其部分和满足\(a_1+a_2+\ldots+a_k\ge{}0,0\le{}k\le{}2n\)的序列个数等于第n个Catalan数\(C_n\)。

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.

用了一个栈去实现。当前哪果是右括号,那么栈顶必须是对应的左括号才行。栈为空的话,也只能push左括号。

 class Solution {
public:
bool isValid(string s) {
if (s.empty()) return true; stack<char> st; for (int i = ; i < s.length(); ++i) {
if (st.empty()) {
if (s[i] == '(' || s[i] == '[' || s[i] == '{') st.push(s[i]);
else return false;
} else if (s[i] == '(' || s[i] == '[' || s[i] == '{') {
st.push(s[i]);
} else if (s[i] == ')') {
if (st.top() == '(') st.pop();
else return false;
} else if (s[i] == ']') {
if (st.top() == '[') st.pop();
else return false;
} else if (s[i] == '}') {
if (st.top() == '{') st.pop();
else return false;
} else {
return false;
}
}
return st.empty();
}
};

重构一下:

 class Solution {
public:
bool isValid(string s) {
if (s.empty()) return true; stack<char> st; for (int i = ; i < s.length(); ++i) {
if (s[i] == '(' || s[i] == '[' || s[i] == '{') {
st.push(s[i]);
continue;
} else if (st.empty()) {
return false;
} if (s[i] == ')' && st.top() != '(') return false;
if (s[i] == ']' && st.top() != '[') return false;
if (s[i] == '}' && st.top() != '{') return false;
st.pop();
}
return st.empty();
}
};

用map再重构,可以再简洁一些。

 class Solution {
public:
bool isValid(string s) {
if (s.empty()) return true; map<char, char> pars;
pars[')'] = '(';
pars[']'] = '[';
pars['}'] = '{'; stack<char> st; for (int i = ; i < s.length(); ++i) {
if (pars.find(s[i]) == pars.end()) {
st.push(s[i]);
continue;
} if (st.empty()) {
return false;
} if (st.top() != pars[s[i]]) return false;
st.pop();
}
return st.empty();
}
};

Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.

For "(()", the longest valid parentheses substring is "()", which has length = 2.

Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.

一开始就把思路定在,当碰到一个“(”右括号时,判断当前已经能够消掉的位置。后面也想过把值和位置作一个新的struct push到stack中,但是怎么就是想不到直接在stack中存位置呢。。。。

应该多思考一下和前面Valid Parentheses的关系。

总结思路就在于:

1. 栈顶为"(",当前字符为")",这时就可出栈,同时记录当前消掉的长度;栈如果为空的话表明前面的符号全部被消掉了,所以长度就为i+1. 否则就是pop之后的新栈顶到当前位置的距离。也就是st.pop(); i - st.top();

2. 要检查栈顶,那么st不能为空。

 class Solution {
public:
int longestValidParentheses(string s) {
if (s.empty()) return ; int max = ;
stack<int> st; for (int i = ; i < s.length(); ++i) {
if (st.empty()) {
st.push(i);
} else {
if (s[st.top()] == '(' && s[i] == ')') {
st.pop();
if (st.empty()) {
if (i + > max) max = i + ;
} else if (i - st.top() > max) {
max = i - st.top();
}
} else {
st.push(i);
}
}
} return max;
}
};

第三次写,用栈。

 class Solution {
public:
int longestValidParentheses(string s) {
if (s.empty()) return ;
stack<int> st; int max = ;
for (int i = ; i < s.length(); i++) {
if (st.empty() || s[i] == '(') {
st.push(i);
} else if (s[st.top()] == '(') {
st.pop();
int len = st.empty() ? i + : i - st.top();
if (len > max) max = len;
} else {
st.push(i);
}
} return max;
}
};

这种求longest、maximum之类的题大多数也是可以用dp来做。

dp[i]表示到第i个位置的最长合法串。

1. 初始值都为0. 第一个符号无影响,所以可以从第二个位置开始。

2. 举例说一下dp[i]的更新,假设s="()(()())"。

i=7时,"()(()())";

dp[i-1]=4就是到i-1=6为止的合法串长度,也就是"()(()())";

此时需要检查j=i-dp[i-1]-1= 7-dp[6]-1=2的位置是否为"(","()(()())";

如果是,那么dp[i]=dp[i-1]+2,dp[7]=dp[6]+2=6;

此时还要把j之前的合法串合并起来。dp[i]+=dp[j-1], "()(()())",dp[7]+=dp[1]; dp[7]=8;

所以答案就是8.

 class Solution {
public:
int longestValidParentheses(string s) {
if (s.empty()) return ; vector<int> dp(s.length(), ); int max = ;
for (int i = ; i < s.length(); ++i) {
int j = i - dp[i - ] - ;
if (s[i] == ')' && s[j] == '(') {
dp[i] = dp[i - ] + ;
if (j > ) {
dp[i] += dp[j - ];
}
if (dp[i] > max) max = dp[i];
}
}
return max;
}
};

Leetcode | Parentheses 相关的更多相关文章

  1. leetcode tree相关题目总结

    leetcode tree相关题目小结 所使用的方法不外乎递归,DFS,BFS. 1. 题100 Same Tree Given two binary trees, write a function ...

  2. [LeetCode] [链表] 相关题目总结

    刷完了LeetCode链表相关的经典题目,总结一下用到的技巧: 技巧 哑节点--哑节点可以将很多特殊case(比如:NULL或者单节点问题)转化为一般case进行统一处理,这样代码实现更加简洁,优雅 ...

  3. [leetcode] 根据String数组构造TreeNode,用于LeetCode树结构相关的测试用例

    LeetCode 跟树结构相关的题目的测试用例中大多是通过String数组来构造树.例如{2,#,3,#,4,#,5,#,6},可以构造出如下的树(将树结构逆时针选择90度显示): 6         ...

  4. leetcode String相关

    目录 3无重复字符的最长子串 5最长回文子串 8字符串转换整数(atoi), 9回文数,7整数反转 28实现strStr(), 459重复的子字符串(KMP) 43字符串相乘 71简化路径 93复原I ...

  5. [LeetCode] 二叉树相关题目(不完全)

    最近在做LeetCode上面有关二叉树的题目,这篇博客仅用来记录这些题目的代码. 二叉树的题目,一般都是利用递归来解决的,因此这一类题目对理解递归很有帮助. 1.Symmetric Tree(http ...

  6. LeetCode - 排列相关题目

    1.获取全排列 https://leetcode.com/problems/permutations/submissions/ 按字典序输出: 这里用的是vector<int>,不是引用. ...

  7. leetcode链表相关

    目录 2/445两数相加 综合题(328奇偶链表, 206反转链表, 21合并两个有序链表 ) 92反转链表 II 链表排序(148排序链表, 876链表的中间结点) 142环形链表 II 160相交 ...

  8. leetCode 字符串相关问题

    125. 验证回文串 /* * 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. * 输入: "A man, a plan, a canal: Panama& ...

  9. leetcode 链表相关

    1.给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们 ...

随机推荐

  1. 17.Python笔记之memcached&redis

    作者:刘耀 博客:www.liuyao.me 博客园:www.cnblogs.com/liu-yao 一.Memcached 1.介绍 Memcached 是一个高性能的分布式内存对象缓存系统,用于动 ...

  2. Emacs 24.3 配置JDEE(http://blog.csdn.net/csfreebird/article/details/19033939)

    最近要重回Java编程,所以打算在最新版本的Emacs 24.3上配置JDEE,听说会有些问题,特此记录安装过程. Emacs 24.3内置了CEDET, 版本是2.0, 这是一个让人困惑的事情,因为 ...

  3. HDU 5795 A Simple Nim (博弈) ---2016杭电多校联合第六场

    A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  4. HDU 4059 容斥原理+快速幂+逆元

    E - The Boss on Mars Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  5. JavaScript、Jquery选择题

    尚学堂Java EE软件工程师认证考试 试题库-选择题     一.    选择题(包括单选和双选) 1.B 在JavaScript中,以下变量命名非法的是(   )(选择一项) A. numb_1 ...

  6. Netbeans快捷键

    一.常用快捷键:1.在文件中查找指定内容 Ctrl+F2.在文件中替换指定内容 Ctrl+H3.在整个项目中查找指定内容 Ctrl+Shift+f4.自动复制整行代码 Ctrl+Shift+上/下方向 ...

  7. 解决 g++ error:/usr/lib/rpm/redhat/redhat-hardened-cc1 No that file and directory

    You need to install redhat-rpm-config which is required by some of the qt switches, probably: sudo d ...

  8. 神经网络(luogu 1038 答案错误,出题人语体教)

    题目背景 人工神经网络(Artificial Neural Network)是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款风险评估等诸多领域有广泛的应用.对神经网络的研究一直是当今 ...

  9. ANT 环境搭建

      一.ant的下载 ant是Apache的一个项目(http://ant.apache.org/),目前的最新版是1.8.4(http://ant.apache.org/bindownload.cg ...

  10. sqlserver临时启用和关闭约束

    select  'ALTER TABLE ['  + b.name +  '] NOCHECK CONSTRAINT ' +  a.name +';' as  禁用约束   from  sysobje ...