【LeetCode】022. 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:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]

  

题解:

Solution 1 ()

class Solution {
public:
    void dfs(int n, int level, int sum, vector<string>& vs, string& s) {
        if(sum<0) {
            sum = 0;
            return;
        }
        if(level >= n) {
            if(sum !=0 ) return;
            vs.push_back(s);
            return;
        }
        s.push_back(‘(‘);
        sum++;
        dfs(n, level+1, sum, vs, s);
        s.pop_back();
        sum--;

        s.push_back(‘)‘);
        sum--;
        dfs(n, level+1, sum, vs, s);
        s.pop_back();
        sum++;
    }
    vector<string> generateParenthesis(int n) {
        vector<string> vs;
        string s;
        if(n<=0) return vs;
        dfs(2*n, 0, 0, vs, s);
        return vs;
    }
};
时间: 2024-08-05 22:46:45

【LeetCode】022. Generate Parentheses的相关文章

【LeetCode】22. Generate Parentheses (I thought I know Python...)

I thought I know Python... Actually , I know nothing... 这个题真想让人背下来啊,每一句都很帅!!! 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

题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. 解题分析: 这类题一般都要用递归的方法来解决.需要设两个集合类分别存储待匹配的(,)的个数. 这里需要明白一点:当待匹配的(的个数永远不小于待匹配的)的个数时只能匹配(,否则会导致错误.(可以自己在纸上试一下就好理解了),其余情况可以考虑匹配( 和)两种情况下可能的结果. 具体代

【一天一道LeetCode】#22. Generate Parentheses

一天一道LeetCode (一)题目 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】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

【Leetcode】Remove Invalid Parentheses

题目链接:https://leetcode.com/problems/remove-invalid-parentheses/ 题目: Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parent

【leetcode】Longest Valid Parentheses

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

【LeetCode】20. Valid Parentheses

题目: 思路:用Stack基本操作完成. 要检测输入字符是否满足这个条件,一个非常合适的数据结构是stack,后进先出的特征正好满足检测的需求.在检测的时候,每次检查一个字符,如果是左括号,就入栈,如果是右括号,并且右括号和当前栈顶符号是左右配对的,那么就弹出栈顶并且进行下一次检测,如果不满足上面两种情况,就说明检查到了一个非法字符,返回false. public class Solution { public boolean isValid(String s) { if(s.length()=

【leetcode 】20. Valid Parentheses

这题需要注意: 栈的空指针 全局变量stack需要clear 输入结束时栈长度不为空 public class Solution { private static final Stack<Character> stack = new Stack<Character>(); public boolean isValid(String s) { stack.clear(); for (int i = 0; i < s.length(); ++i) { char c = s.cha

【leetcode】Generate Parentheses

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