LeetCode 22. Generate Parentheses (括号生成)

题目标签:Backtracking

  建立一个 HashMap 来记录 括号的 数量,利用DFS, 先用 左括号, 在用 右括号, 当 右括号用完的时候 返回。具体看code。

Java Solution:

Runtime:  1 ms, faster than 85.94 %

Memory Usage: 39.5 MB, less than 20.00 %

完成日期:12/12/2019

关键点:HashMap

class Solution {
    HashMap<Character, Integer> map;

    public List<String> generateParenthesis(int n) {
        map = new HashMap<>();
        List<String> res = new ArrayList<>();
        StringBuilder comb = new StringBuilder();

        map.put(‘(‘, n);
        map.put(‘)‘, n);

        DFS(res, comb);

        return res;
    }

    private void DFS(List<String> res, StringBuilder comb) {
        if(map.get(‘)‘) == 0) {
            res.add(comb.toString());
            return;
        }

        // append ‘(‘
        if(map.get(‘(‘) > 0) {
            comb.append(‘(‘);
            map.put(‘(‘, map.get(‘(‘) - 1);
            DFS(res, comb);
            comb.deleteCharAt(comb.length()-1);
            map.put(‘(‘, map.get(‘(‘) + 1);
        }

        // append ‘)‘ only when ( is used first
        if(map.get(‘(‘) < map.get(‘)‘) && map.get(‘)‘) > 0) {
            comb.append(‘)‘);
            map.put(‘)‘, map.get(‘)‘) - 1);
            DFS(res, comb);
            comb.deleteCharAt(comb.length()-1);
            map.put(‘)‘, map.get(‘)‘) + 1);
        }
    }
}

参考资料:n/a

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

原文地址:https://www.cnblogs.com/jimmycheng/p/12495796.html

时间: 2024-11-07 22:14:54

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

LeetCode22 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 Java

题目: 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: [ "((()))", "(()())", "(())()", "()(())", "()()()" ]

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

leetcode 22 Generate Parentheses

class Solution { public: vector<string> generateParenthesis(int n) { set<string> t; if (n == 0) t.insert(""); else { vector<string> pre = generateParenthesis(n - 1); for (auto a : pre) { for (int i = 0; i < a.size(); ++i) {

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】Generate Parentheses

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

No.22 Generate Parentheses

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