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

来源:互联网 发布:java图像压缩算法 编辑:程序博客网 时间:2024/04/28 22:55

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.html

http://www.2cto.com/kf/201310/251552.html

http://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());            }            else                lastleft = 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+1                    if(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 "()[]{}" are all valid but "(]" and "([)]" are not.

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]);       else       stk.pop();}return stk.empty();    }    bool ispair(char ch1,char ch2)    {    return ((ch1 == ')' && ch2 == '(')||(ch1 == '}' && ch2 == '{')||(ch1 == ']' && ch2 == '[')); }};







0 0
原创粉丝点击