LeetCode4FLAG

🔥 🔥 High frequent interview LeetCode 100 for FaceBook,Linkedin,Amazon,Google,Microsoft. More importantly, the problems' solutions are provided,include C++, Python and Java.

Stars
112
Committers
2

🌍 English | 简体中文| 日本語 | Українською


Note: Please raise an issue for any suggestions, corrections, and feedback.

Index

Top100

Index

Classification

Index

算法思维+算法模板

回溯

result = []
def backtrack(路径, 选择列表):
    if 满足结束条件:
        result.add(路径)
        return

    for 选择 in 选择列表:
        做选择
        backtrack(路径, 选择列表)
        撤销选择

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

Example 1:

Input: n = 3
Output: ["((()))","(()())","(())()","()(())","()()()"]

Example 2:

Input: n = 1
Output: ["()"]

Constraints:

1 <= n <= 8

void backtrack(int n, int i, string& track) {
    // i 代表当前的位置,共 2n 个位置
    // 穷举到最后一个位置了,得到一个长度为 2n 组合
    if (i == 2 * n) {
        print(track);
        return;
    }

    // 对于每个位置可以是左括号或者右括号两种选择
    for choice in ['(', ')'] {
        track.push(choice); // 做选择
        // 穷举下一个位置
        backtrack(n, i + 1, track);
        track.pop(choice); // 撤销选择
    }
}

See more -> 题解模板.md

Index

Community

Index

License

Index

🚀 知识星球

专为求职面试中算法与数据结构的小伙伴,创了学习交流/刷题群(知识星球)!想要最快的提升算法与数据结构技能,和更多小伙伴一起来吧!进阶面试八股文,大厂面经

💖 Support this project

Donating to help me continue working on this project.

    |

Badges
Extracted from project README
Discord