150 principais entrevistasFácil

Gerar parênteses

Guia detalhado e implementação de Python para o problema 'Gerar Parênteses'.

Declaração do problema

Fácil

Dados n pares de parênteses, escreva uma função para gerar todas as combinações de parênteses bem formados.

Escreva uma função generateParenthesis(n: int) -> List[str].

Restrições
  • 1 <= n <= 8

Exemplos

Example 1
Input
n = 3
Output
["((()))", "(()())", "(())()", "()(())", "()()()"]
Explanation

All 5 valid combinations of 3 pairs of parentheses.

Example 2
Input
n = 1
Output
["()"]
Explanation

Only one valid combination with 1 pair.

Need a Hint?
Considere usar estruturas de dados específicas da pilha, como conjuntos ou heaps.
Edge Cases to Watch
  • Estruturas de entrada vazias
  • Entradas de elemento único
  • Grandes limites numéricos

Pronto para resolver?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Abrir no Editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Recursos Python recomendados

Expanda seu conhecimento com tutoriais interativos relacionados, folhas de dicas e comparações de código.