码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】Generate Parentheses (2 solutions)

时间:2014-12-20 11:35:05      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

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:

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

 

解法一:递归

借助栈,‘(‘、‘)‘构成一对分别进出栈。最后栈为空,则输入括号构成的字符串是合法的。

class Solution {
public:
    vector<string> generateParenthesis(int n) {
        vector<string> result;
        if(n == 0)
            return result;
        //first must be ‘(‘
        string cur = "(";
        stack<char> s;
        s.push(();
        
        Helper(result, cur, s, 2*n-1);
        return result;
    }
    void Helper(vector<string>& result, string cur, stack<char> s, int num)
    {
        if(num == 1)
        {//must be ‘)‘
            if(s.top() == ( && s.size() == 1)
            {//all matched
                cur += );
                result.push_back(cur);
            }
        }
        else
        {
            //‘(‘ always push
            string str1 = cur;
            str1 += (;
            s.push(();
            Helper(result, str1, s, num-1);
            s.pop();
            
            //‘)‘
            if(!s.empty())
            {//prune. never begin with ‘)‘
                string str2 = cur;
                str2 += );
                if(s.top() == ()
                    s.pop();    //check empty() before access top()
                else
                    s.push());
                Helper(result, str2, s, num-1);
            }
        }
    }
};

技术分享

 

解法二:递归

稍作分析可知,栈是不必要的,只要记录字符串中有几个‘(‘,记为count。

每进入一个‘(‘, count ++. 每匹配一对括号, count--。

最终全部匹配,需要count==0

class Solution {
public:
    vector<string> generateParenthesis(int n) {
        vector<string> result;
        if(n == 0)
            return result;
        //first must be ‘(‘
        string cur = "(";
        int count = 1;  //number of (‘s in cur
        
        Helper(result, cur, count, 2*n-1);
        return result;
    }
    void Helper(vector<string>& result, string cur, int count, int num)
    {
        if(num == 1)
        {//must be ‘)‘
            if(count == 1)
            {//all matched
                cur += );
                result.push_back(cur);
            }
        }
        else
        {
            //‘(‘ always push
            string str1 = cur;
            str1 += (;
            count ++;
            Helper(result, str1, count, num-1);
            count --;
            
            //‘)‘
            if(count != 0)
            {//prune. never begin with ‘)‘
                string str2 = cur;
                str2 += );
                count --;
                Helper(result, str2, count, num-1);
            }
        }
    }
};

技术分享

【LeetCode】Generate Parentheses (2 solutions)

标签:

原文地址:http://www.cnblogs.com/ganganloveu/p/4175159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!