您的位置:首页 > 其它

【LeetCode】Valid Parentheses & Generate Parentheses & Longest Valid Parentheses

2014-04-22 16:13 477 查看

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:
"((()))", "(()())", "(())()",
"()(())", "()()()"
参考:http://www.cnblogs.com/remlostime/archive/2012/11/06/2757711.htmlhttp://www.2cto.com/kf/201310/251552.htmlhttp://blog.csdn.net/pickless/article/details/9141935思路:
class Solution {
public:
vector<string> generateParenthesis(int n) {
string str;
vector<string> ret;
generateParenthesisCore(ret,"",0,0,n);
return ret;
}
void generateParenthesisCore(vector<string> &ret,string str,int lnum,int rnum,const int n)
{
if(lnum > n)	return;
if(lnum + rnum == 2*n)
ret.push_back(str);

generateParenthesisCore(ret,str+'(',lnum + 1,rnum,n);
if(lnum > rnum)
generateParenthesisCore(ret,str+')',lnum,rnum+1,n);
}
};

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.参考:http://blog.csdn.net/jellyyin/article/details/9887959
class Solution {public:int longestValidParentheses(string s) {stack<int> stk;///////////////////////////////int maxlen = 0, lastleft = -1;int i = 0;while(i<s.size()){if(s[i] == '('){stk.push(i);}else if(s[i] == ')' && !stk.empty()){stk.pop();if(stk.empty()) maxlen = max(maxlen,i-lastleft);/////////////////////////////////else            maxlen = max(maxlen,i-stk.top());}elselastleft = i;////////////////////////////////i++;////////////////////////////////////}return maxlen;}};
http://blog.csdn.net/doc_sgl/article/details/12252443
</pre><pre name="code" class="cpp">class Solution {public:int longestValidParentheses(string s) {int size = s.size();if(size < 2) return 0;///////////////////////////特殊处理vector<int> longest(size,0);int maxl = 0;for(int i = size-2;i>=0;i--)////////////////从size-2开始{if(s[i] == '('){int j = i+1+longest[i+1];//去除本配好对的下一个符号if(j< size && s[j] == ')'){longest[i] = longest[i+1] + 2;//////////////////////////////////////    i+1if(j+1<size)longest[i] += longest[j+1];//加上下一个段}maxl = max(maxl,longest[i]);///////////////////////////////////////////// 更新}//printf("%c i = %d, lo = %d, max = %d\n",s[i],i,longest[i],maxl);}return maxl;}};

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 
"()[]{}"
 areall valid but 
"(]"
 and 
"([)]"
 arenot.
class Solution {public:bool isValid(string s) {int size = s.size();stack<char> stk;while(size--){if(stk.size() == 0 || !ispair(stk.top(),s[size]))stk.push(s[size]);elsestk.pop();}return stk.empty();}bool ispair(char ch1,char ch2){return ((ch1 == ')' && ch2 == '(')||(ch1 == '}' && ch2 == '{')||(ch1 == ']' && ch2 == '['))	;}};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: