LeetCode 20 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:

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

还是类似的括号匹配问题,这次是要生成所有可能的匹配组合,思路如下:

首先用迭代法穷举可能出现的所有情况,然后利用已有的isValid()判断是否匹配,进而决定是否储存到返回vector容器中。

class Solution {
public:
    vector<string> generateParenthesis(int n)
    {
        mSize = n * 2;
        init();
        string s;
        genpa(-1, s);
        return ret;
    }
    bool isValid(string s)
{
    stack<char> stk;
    int nSize = s.size();

    for(int i = 0; i!= nSize; ++i)
    {
        if(s[i] == ‘{‘ || s[i] == ‘(‘ || s[i]== ‘[‘)
        {
            stk.push(s[i]);
        }
        else
        {
            if(stk.empty())
            {
                return false;
            }
            char cElem = stk.top();
            if(cElem - s[i] ==-1 || cElem - s[i] == -2)
            {
                stk.pop();
                continue;
            }
            else
            {
                return false;
            }
        }
    }
    if(!stk.empty())
    {
        return false;
    }
    return true;
}
    void genpa(int j, string s)
    {
        if(j == mSize -1)
        {
            if(isValid(s))
            {
                ret.push_back(s);
            }
            return;
        }
        for(int i = 0; i!= 2; ++i)
        {
            genpa(j+1, s+album[i]);
        }
    }
    void init(void)
    {
        album[0] = ‘(‘;
        album[1] = ‘)‘;
    }
private:
    int mSize;
    vector<string> ret;
    char album[2];
};

思路类似,多举一反三。

时间: 2024-08-29 12:46:29

LeetCode 20 Generate Parentheses的相关文章

[LeetCode] 22. 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: "((()))", "(()())", "(())()", "()(())", "()()()" 给一个数字n,

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

Leetcode 暴力 Generate Parentheses

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Generate Parentheses Total Accepted: 11470 Total Submissions: 37301 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3

[LeetCode]20 Valid Parentheses 有效的括号

[LeetCode]20 Valid Parentheses 有效的括号 Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brac

[LeetCode][JavaScript]Generate Parentheses

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: "((()))", "(()())", "(())()", "()(())", "

leetCode 22.Generate Parentheses (生成括号) 解题思路和方法

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: "((()))", "(()())", "(())()", "()(())", "

leetCode 20. Valid Parentheses 字符串

20. 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 "(]&quo

【LeetCode】Generate Parentheses (2 solutions)

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: "((()))", "(()())", "(())()", "()(())", "

LeetCode 022 Generate Parentheses

题目描述: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: "((()))", "(()())", "(())()", "()(())",